--- /home/xd1000/xd1000/etherboot-5.4.2/src/filo/drivers/ide_x.c	2006-06-08 15:54:56.000000000 -0600
+++ filo_combined.c	2007-09-17 11:50:07.000000000 -0600
@@ -1,14 +1,28 @@
 /* Derived from Etherboot 5.1 */
 
-#include <etherboot.h>
-#include <pci.h>
-#include <timer.h>
-#include <lib.h>
+// MY GARBAGE MGW
 
-#define DEBUG_THIS DEBUG_IDE
-#include <debug.h>
+#define uint8_t unsigned char
+#define uint16_t unsigned short
+#define uint32_t unsigned long
+#define sector_t unsigned long
+#define size_t unsigned short
 
-#define IDE_MAX_CONTROLLERS 4
+#define ndelay(a) mdelay(1)
+#define msleep(a) mdelay(a)
+
+#define outb_filo(x,y)  outb(y,x)
+#define outw_filo(x,y)  outw(y,x)
+#define debug(a...)	    BX_DEBUG_ATA(a)
+#define debugx(a...)    BX_DEBUG_ATA(a)
+
+#define TIMEOUT 0
+#define BSY	1
+#define NOT_BSY 2
+
+// END MY GARBAGE
+
+#define IDE_MAX_CONTROLLERS 2
 #define IDE_MAX_DRIVES (IDE_MAX_CONTROLLERS * 2)
 
 #define BSY_SET_DURING_SPINUP 1
@@ -33,13 +47,11 @@
  * 
  *
  */
-struct controller {
-	uint16_t cmd_base;
-	uint16_t ctrl_base;
-};
+typedef struct {
+	   uint16_t cmd_base;
+} controller_t;
 
-struct harddisk_info {
-	struct controller *ctrl;
+typedef struct {
 	uint16_t heads;
 	uint16_t cylinders;
 	uint16_t sectors_per_track;
@@ -52,10 +64,10 @@
 #define ADDRESS_MODE_LBA48  2
 #define ADDRESS_MODE_PACKET 3
 	uint32_t hw_sector_size;
-	unsigned drive_exists : 1;
-	unsigned slave_absent : 1;
-	unsigned removable : 1;
-};
+	uint8_t drive_exists;
+	uint8_t slave_absent;
+	uint8_t removable;
+}harddisk_info_t;
 
 #define IDE_SECTOR_SIZE 0x200
 #define CDROM_SECTOR_SIZE 0x800
@@ -69,25 +81,25 @@
 
 #define IDE_REG_EXTENDED_OFFSET   (0x204u)
 
-#define IDE_REG_DATA(ctrl)           ((ctrl)->cmd_base + 0u) /* word register */
-#define IDE_REG_ERROR(ctrl)          ((ctrl)->cmd_base + 1u)
-#define IDE_REG_PRECOMP(ctrl)        ((ctrl)->cmd_base + 1u)
-#define IDE_REG_FEATURE(ctrl)        ((ctrl)->cmd_base + 1u)
-#define IDE_REG_SECTOR_COUNT(ctrl)   ((ctrl)->cmd_base + 2u)
-#define IDE_REG_SECTOR_NUMBER(ctrl)  ((ctrl)->cmd_base + 3u)
-#define IDE_REG_LBA_LOW(ctrl)        ((ctrl)->cmd_base + 3u)
-#define IDE_REG_CYLINDER_LSB(ctrl)   ((ctrl)->cmd_base + 4u)
-#define IDE_REG_LBA_MID(ctrl)	     ((ctrl)->cmd_base + 4u)
-#define IDE_REG_CYLINDER_MSB(ctrl)   ((ctrl)->cmd_base + 5u)
-#define IDE_REG_LBA_HIGH(ctrl)	     ((ctrl)->cmd_base + 5u)
-#define IDE_REG_DRIVEHEAD(ctrl)      ((ctrl)->cmd_base + 6u)
-#define IDE_REG_DEVICE(ctrl)	     ((ctrl)->cmd_base + 6u)
-#define IDE_REG_STATUS(ctrl)         ((ctrl)->cmd_base + 7u)
-#define IDE_REG_COMMAND(ctrl)        ((ctrl)->cmd_base + 7u)
-#define IDE_REG_ALTSTATUS(ctrl)      ((ctrl)->ctrl_base + 2u)
-#define IDE_REG_DEVICE_CONTROL(ctrl) ((ctrl)->ctrl_base + 2u)
+#define IDE_REG_DATA(base)           (base + 0u) /* word register */
+#define IDE_REG_ERROR(base)          (base + 1u)
+#define IDE_REG_PRECOMP(base)        (base + 1u)
+#define IDE_REG_FEATURE(base)        (base + 1u)
+#define IDE_REG_SECTOR_COUNT(base)   (base + 2u)
+#define IDE_REG_SECTOR_NUMBER(base)  (base + 3u)
+#define IDE_REG_LBA_LOW(base)        (base + 3u)
+#define IDE_REG_CYLINDER_LSB(base)   (base + 4u)
+#define IDE_REG_LBA_MID(base)	     (base + 4u)
+#define IDE_REG_CYLINDER_MSB(base)   (base + 5u)
+#define IDE_REG_LBA_HIGH(base)	     (base + 5u)
+#define IDE_REG_DRIVEHEAD(base)      (base + 6u)
+#define IDE_REG_DEVICE(base)	     (base + 6u)
+#define IDE_REG_STATUS(base)         (base + 7u)
+#define IDE_REG_COMMAND(base)        (base + 7u)
+#define IDE_REG_ALTSTATUS(base)      (base + IDE_REG_EXTENDED_OFFSET + 2u)
+#define IDE_REG_DEVICE_CONTROL(base) (base + IDE_REG_EXTENDED_OFFSET + 2u)
 
-struct ide_pio_command
+typedef struct
 {
 	uint8_t feature;
 	uint8_t sector_count;
@@ -106,7 +118,7 @@
 	uint8_t lba_low2;
 	uint8_t lba_mid2;
 	uint8_t lba_high2;
-};
+} ide_pio_command;
 
 #define IDE_DEFAULT_COMMAND { 0xFFu, 0x01, 0x00, 0x0000, IDE_DH_DEFAULT }
 
@@ -211,25 +223,44 @@
 #define IDE_FEATURE_ENABLE_REVERTING_TO_POWERON_DEFAULTS    0xCC
 #define IDE_FEATURE_DISABLE_SERVICE_INTERRUPT               0xDE
 
-static unsigned short ide_base[] = {
-	IDE_BASE0,
-	IDE_BASE1, 
-	IDE_BASE2, 
-	IDE_BASE3, 
-	0
-};
-
-static struct controller controllers[IDE_MAX_CONTROLLERS];
-static struct harddisk_info harddisk_info[IDE_MAX_DRIVES];
-
-static unsigned char ide_buffer[IDE_SECTOR_SIZE];
+static int await_ide();
+static void print_status();
+static int ide_software_reset();
+static void pio_set_registers();
+static int pio_non_data();
+static int pio_data_in();
+static int pio_packet();
+static int ide_read_sector_chs();
+static int ide_read_sector_lba();
+static int ide_read_sector_lba48();
+static int ide_read_sector_packet();
+int ide_read();
+static int init_drive_x();
+static int ide_bus_floating();
+static int init_controller();
+static int atapi_request_sense();
+static int detect_medium();
+static int find_ide_controller();
+int ide_probe();
+int ide_probe_all();
+/* Derived from Etherboot 5.1 */
 
-static int await_ide(int (*done)(struct controller *ctrl), 
-	struct controller *ctrl, unsigned long timeout)
+static int await_ide(when_done,base,timeout)
+	   int when_done;
+	   int base;
+	   unsigned int timeout;
 {
+	unsigned int time_lo=0, time_hi=0;
 	int result;
 	for(;;) {
-		result = done(ctrl);
+	   time_lo++;
+	   if (when_done == BSY)
+		result = inb(IDE_REG_STATUS(base)) & IDE_STATUS_BSY;
+	   else if (when_done == NOT_BSY)
+		result = !(inb(IDE_REG_STATUS(base)) & IDE_STATUS_BSY);
+	   else if (when_done == TIMEOUT)
+	     result = 0;
+	   
 #if IDE_DISK_POLL_DELAY
 		mdelay(1);
 #endif
@@ -237,7 +268,14 @@
 			return 0;
 		}
 		//poll_interruptions();
-		if ((timeout == 0) || (currticks() > timeout)) {
+		if (time_lo == 0x800) // mod 2048 each ~2 seconds
+		{
+			 time_hi++;
+			 time_lo = 0;
+		    debug("waiting type (TIMEOUT,BSY,NOT_BSY) %d time= %d timeout= %d\n",when_done,time_hi, timeout);
+	         
+          }
+		if ((timeout == 0) || (time_hi > timeout)) {
 			break;
 		}
 	}
@@ -248,36 +286,21 @@
 /* The maximum time any IDE command can last 31 seconds,
  * So if any IDE commands takes this long we know we have problems.
  */
-#define IDE_TIMEOUT (32*TICKS_PER_SEC)
-
-static int not_bsy(struct controller *ctrl)
-{
-	return !(inb(IDE_REG_STATUS(ctrl)) & IDE_STATUS_BSY);
-}
+#define IDE_TIMEOUT 32*1000
 
 /* IDE drives assert BSY bit within 400 nsec when SRST is set.
- * Use 2 msec since our tick is 1 msec */
-#define IDE_RESET_PULSE (2*TICKS_PER_SEC / 1000)
+ * Use 1 msec */
+#define IDE_RESET_PULSE 1
 
-static int bsy(struct controller *ctrl)
+static void print_status(base)
+	   unsigned int base;
 {
-	return inb(IDE_REG_STATUS(ctrl)) & IDE_STATUS_BSY;
+	debug("IDE: status=%x, err=%x\n",
+			inb(IDE_REG_STATUS(base)), inb(IDE_REG_ERROR(base)));
 }
 
-#if  !BSY_SET_DURING_SPINUP
-static int timeout(struct controller *ctrl)
-{
-	return 0;
-}
-#endif
-
-static void print_status(struct controller *ctrl)
-{
-	debug("IDE: status=%#x, err=%#x\n",
-			inb(IDE_REG_STATUS(ctrl)), inb(IDE_REG_ERROR(ctrl)));
-}
-
-static int ide_software_reset(struct controller *ctrl)
+static int ide_software_reset(base)
+unsigned int base;
 {
 	/* Wait a little bit in case this is immediately after
 	 * hardware reset.
@@ -287,39 +310,41 @@
 	 * is set.  If the bsy bit does not clear in a reasonable
 	 * amount of time give up.
 	 */
-	debug("Waiting for ide%d to become ready for reset... ",
-			ctrl - controllers);
-	if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
+	debug("Waiting for ide to become ready for reset... ");
+	if (await_ide(NOT_BSY, base, IDE_TIMEOUT) < 0) {
 		debug("failed\n");
 		return -1;
 	}
 	debug("ok\n");
 
 	/* Disable Interrupts and reset the ide bus */
-	outb(IDE_CTRL_HD15 | IDE_CTRL_SRST | IDE_CTRL_NIEN, 
-		IDE_REG_DEVICE_CONTROL(ctrl));
+	outb_filo(IDE_CTRL_HD15 | IDE_CTRL_SRST | IDE_CTRL_NIEN, 
+		IDE_REG_DEVICE_CONTROL(base));
 	/* If BSY bit is not asserted within 400ns, no device there */
-	if (await_ide(bsy, ctrl, currticks() + IDE_RESET_PULSE) < 0) {
+	if (await_ide(BSY, base, IDE_RESET_PULSE) < 0) {
+		debug("No device\n");
 		return -1;
 	}
-	outb(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(ctrl));
+	outb_filo(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(base));
 	mdelay(2);
-	if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
+	if (await_ide(NOT_BSY, base, IDE_TIMEOUT) < 0) {
+		debug("device never becomes not busy\n");
 		return -1;
 	}
 	return 0;
 }
 
-static void pio_set_registers(
-	struct controller *ctrl, const struct ide_pio_command *cmd)
+static void pio_set_registers(base,cmd)
+	unsigned int base;
+	ide_pio_command *cmd;
 {
 	uint8_t device;
 	/* Disable Interrupts */
-	outb(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(ctrl));
+	outb_filo(IDE_CTRL_HD15 | IDE_CTRL_NIEN, IDE_REG_DEVICE_CONTROL(base));
 
 	/* Possibly switch selected device */
-	device = inb(IDE_REG_DEVICE(ctrl));
-	outb(cmd->device,          IDE_REG_DEVICE(ctrl));
+	device = inb(IDE_REG_DEVICE(base));
+	outb_filo(cmd->device,          IDE_REG_DEVICE(base));
 	if ((device & (1UL << 4)) != (cmd->device & (1UL << 4))) {
 		/* Allow time for the selected drive to switch,
 		 * The linux ide code suggests 50ms is the right
@@ -327,110 +352,128 @@
 		 */
 		mdelay(50); 
 	}
-	outb(cmd->feature,         IDE_REG_FEATURE(ctrl));
+	outb_filo(cmd->feature,         IDE_REG_FEATURE(base));
 	if (cmd->command == IDE_CMD_READ_SECTORS_EXT) {
-		outb(cmd->sector_count2,   IDE_REG_SECTOR_COUNT(ctrl));
-		outb(cmd->lba_low2,        IDE_REG_LBA_LOW(ctrl));
-		outb(cmd->lba_mid2,        IDE_REG_LBA_MID(ctrl));
-		outb(cmd->lba_high2,       IDE_REG_LBA_HIGH(ctrl));
-	}
-	outb(cmd->sector_count,    IDE_REG_SECTOR_COUNT(ctrl));
-	outb(cmd->lba_low,         IDE_REG_LBA_LOW(ctrl));
-	outb(cmd->lba_mid,         IDE_REG_LBA_MID(ctrl));
-	outb(cmd->lba_high,        IDE_REG_LBA_HIGH(ctrl));
-	outb(cmd->command,         IDE_REG_COMMAND(ctrl));
+		outb_filo(cmd->sector_count2,   IDE_REG_SECTOR_COUNT(base));
+		outb_filo(cmd->lba_low2,        IDE_REG_LBA_LOW(base));
+		outb_filo(cmd->lba_mid2,        IDE_REG_LBA_MID(base));
+		outb_filo(cmd->lba_high2,       IDE_REG_LBA_HIGH(base));
+	}
+	outb_filo(cmd->sector_count,    IDE_REG_SECTOR_COUNT(base));
+	outb_filo(cmd->lba_low,         IDE_REG_LBA_LOW(base));
+	outb_filo(cmd->lba_mid,         IDE_REG_LBA_MID(base));
+	outb_filo(cmd->lba_high,        IDE_REG_LBA_HIGH(base));
+	outb_filo(cmd->command,         IDE_REG_COMMAND(base));
 }
 
 
-static int pio_non_data(struct controller *ctrl, const struct ide_pio_command *cmd)
+static int pio_non_data(base, cmd)
+unsigned int base;
+ide_pio_command *cmd;
 {
 	/* Wait until the busy bit is clear */
-	if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
+	if (await_ide(NOT_BSY, base, IDE_TIMEOUT) < 0) {
 		return -1;
 	}
 
-	pio_set_registers(ctrl, cmd);
+	pio_set_registers(base, cmd);
 	ndelay(400);
-	if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
+	if (await_ide(NOT_BSY, base, IDE_TIMEOUT) < 0) {
 		return -1;
 	}
 	/* FIXME is there more error checking I could do here? */
 	return 0;
 }
 
-static int pio_data_in(struct controller *ctrl, const struct ide_pio_command *cmd,
-	void *buffer, size_t bytes)
+static int pio_data_in(base, cmd, buffer, bytes)
+unsigned int base;
+ide_pio_command *cmd;
+void *buffer;
+size_t bytes;
 {
 	unsigned int status;
+	unsigned int word;
 
 	/* FIXME handle commands with multiple blocks */
 	/* Wait until the busy bit is clear */
-	if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
+	if (await_ide(NOT_BSY, base, IDE_TIMEOUT) < 0) {
 		return -1;
 	}
 
 	/* How do I tell if INTRQ is asserted? */
-	pio_set_registers(ctrl, cmd);
+	pio_set_registers(base, cmd);
 	ndelay(400);
-	if (await_ide(not_bsy, ctrl, currticks() + IDE_TIMEOUT) < 0) {
+	if (await_ide(NOT_BSY, base, IDE_TIMEOUT) < 0) {
 		return -1;
 	}
-	status = inb(IDE_REG_STATUS(ctrl));
+	status = inb(IDE_REG_STATUS(base));
 	if (!(status & IDE_STATUS_DRQ)) {
-		print_status(ctrl);
+		print_status(base);
 		return -1;
 	}
-	insw(IDE_REG_DATA(ctrl), buffer, bytes/2);
-	status = inb(IDE_REG_STATUS(ctrl));
+	//insw(IDE_REG_DATA(base), buffer, bytes/2);
+	   for (word = 0; word < bytes/2; word++)
+			 ((unsigned int*)buffer)[word]=inb(IDE_REG_DATA(base));
+	status = inb(IDE_REG_STATUS(base));
 	if (status & IDE_STATUS_DRQ) {
-		print_status(ctrl);
+		print_status(base);
 		return -1;
 	}
 	return 0;
 }
 
-static int pio_packet(struct harddisk_info *info, int in,
-	const void *packet, int packet_len,
-	void *buffer, int buffer_len)
+static int pio_packet(drive,in,packet,packet_len,buffer,buffer_len)
+int drive;
+int in;
+void *packet;
+int packet_len;
+void *buffer;
+int buffer_len;
 {
 	unsigned int status;
-	struct ide_pio_command cmd;
+	ide_pio_command cmd;
+     unsigned int word;
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
+	unsigned int base = read_word(ebda_seg,&EbdaData->controllers[drive>>1].cmd_base);
 
-	memset(&cmd, 0, sizeof(cmd));
+	memsetb(get_SS(),&cmd, 0, sizeof(cmd));
 
 	/* Wait until the busy bit is clear */
-	if (await_ide(not_bsy, info->ctrl, currticks() + IDE_TIMEOUT) < 0) {
+	if (await_ide(NOT_BSY, base, IDE_TIMEOUT) < 0) {
 		return -1;
 	}
 
 	/* Issue a PACKET command */
 	cmd.lba_mid = (uint8_t) buffer_len;
 	cmd.lba_high = (uint8_t) (buffer_len >> 8);
-	cmd.device = IDE_DH_DEFAULT | info->slave;
+	cmd.device = IDE_DH_DEFAULT 
+           | read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave);
 	cmd.command = IDE_CMD_PACKET;
-	pio_set_registers(info->ctrl, &cmd);
+	pio_set_registers(base, &cmd);
 	ndelay(400);
-	if (await_ide(not_bsy, info->ctrl, currticks() + IDE_TIMEOUT) < 0) {
+	if (await_ide(NOT_BSY, base, IDE_TIMEOUT) < 0) {
 		return -1;
 	}
-	status = inb(IDE_REG_STATUS(info->ctrl));
+	status = inb(IDE_REG_STATUS(base));
 	if (!(status & IDE_STATUS_DRQ)) {
 		debug("no drq after PACKET\n");
-		print_status(info->ctrl);
+		print_status(base);
 		return -1;
 	}
 
 	/* Send the packet */
-	outsw(IDE_REG_DATA(info->ctrl), packet, packet_len/2);
+	//outsw(IDE_REG_DATA(base), packet, packet_len/2);
+	   for (word=0;word < packet_len/2; word++)
+			 outw_filo(((unsigned int*)packet)[word],IDE_REG_DATA(base));
 
-	if (await_ide(not_bsy, info->ctrl, currticks() + IDE_TIMEOUT) < 0) {
+	if (await_ide(NOT_BSY, base, IDE_TIMEOUT) < 0) {
 		return -1;
 	}
-	status = inb(IDE_REG_STATUS(info->ctrl));
+	status = inb(IDE_REG_STATUS(base));
 	if (buffer_len == 0) {
 		if (status & IDE_STATUS_DRQ) {
 			debug("drq after non-data command\n");
-			print_status(info->ctrl);
+			print_status(base);
 			return -1;
 		}
 		return 0;
@@ -438,54 +481,67 @@
 
 	if (!(status & IDE_STATUS_DRQ)) {
 		debug("no drq after sending packet\n");
-		print_status(info->ctrl);
+		print_status(base);
 		return -1;
 	}
 
-	insw(IDE_REG_DATA(info->ctrl), buffer, buffer_len/2);
+	//insw(IDE_REG_DATA(base), buffer, buffer_len/2);
+	   for (word = 0; word < buffer_len/2; word++)
+			 ((unsigned int*)buffer)[word]=inb(IDE_REG_DATA(base));
+	   
 
-	status = inb(IDE_REG_STATUS(info->ctrl));
+	status = inb(IDE_REG_STATUS(base));
 	if (status & IDE_STATUS_DRQ) {
 		debug("drq after insw\n");
-		print_status(info->ctrl);
+		print_status(base);
 		return -1;
 	}
 	return 0;
 }
 
-static inline int ide_read_sector_chs(
-	struct harddisk_info *info, void *buffer, unsigned long sector)
+static int ide_read_sector_chs(drive,buffer,sector)
+int drive;
+void *buffer;
+unsigned long sector;
 {
-	struct ide_pio_command cmd;
+	ide_pio_command cmd;
 	unsigned int track;
 	unsigned int offset;
 	unsigned int cylinder;
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
+	unsigned int base = read_word(ebda_seg,&EbdaData->controllers[drive>>1].cmd_base);
 		
-	memset(&cmd, 0, sizeof(cmd));
+	memsetb(get_SS(),&cmd, 0, sizeof(cmd));
 	cmd.sector_count = 1;
 
 	//debug("ide_read_sector_chs: sector= %ld.\n",sector);
 
-	track = sector / info->sectors_per_track;
+	track = sector / read_word(ebda_seg,&EbdaData->harddisk_info[drive].sectors_per_track);
 	/* Sector number */
-	offset = 1 + (sector % info->sectors_per_track);
-	cylinder = track / info->heads;
+	   //Myles
+	offset = 1 + (sector -(track*read_word(ebda_seg,&EbdaData->harddisk_info[drive].sectors_per_track)));
+
+	cylinder = track / read_word(ebda_seg,&EbdaData->harddisk_info[drive].heads);
 	cmd.lba_low = offset;
 	cmd.lba_mid = cylinder & 0xff;
 	cmd.lba_high = (cylinder >> 8) & 0xff;
 	cmd.device = IDE_DH_DEFAULT |
-		IDE_DH_HEAD(track % info->heads) |
-		info->slave |
+		IDE_DH_HEAD(track - (cylinder*read_word(ebda_seg,&EbdaData->harddisk_info[drive].heads))) |
+          read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave) |
 		IDE_DH_CHS;
 	cmd.command = IDE_CMD_READ_SECTORS;
-	return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
+	return pio_data_in(base, &cmd, buffer, IDE_SECTOR_SIZE);
 }
 
-static inline int ide_read_sector_lba(
-	struct harddisk_info *info, void *buffer, unsigned long sector)
-{
-	struct ide_pio_command cmd;
-	memset(&cmd, 0, sizeof(cmd));
+static int ide_read_sector_lba(drive,buffer,sector)
+int drive;
+void *buffer;
+unsigned long sector;
+{
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
+	unsigned int base = read_word(ebda_seg,&EbdaData->controllers[drive>>1].cmd_base);
+	ide_pio_command cmd;
+	memsetb(get_SS(),&cmd, 0, sizeof(cmd));
 
 	cmd.sector_count = 1;
 	cmd.lba_low = sector & 0xff;
@@ -493,18 +549,22 @@
 	cmd.lba_high = (sector >> 16) & 0xff;
 	cmd.device = IDE_DH_DEFAULT |
 		((sector >> 24) & 0x0f) |
-		info->slave | 
+          read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave) |
 		IDE_DH_LBA;
 	cmd.command = IDE_CMD_READ_SECTORS;
 	//debug("%s: sector= %ld, device command= 0x%x.\n",__FUNCTION__,(unsigned long) sector, cmd.device);
-	return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
+	return pio_data_in(base, &cmd, buffer, IDE_SECTOR_SIZE);
 }
 
-static inline int ide_read_sector_lba48(
-	struct harddisk_info *info, void *buffer, sector_t sector)
-{
-	struct ide_pio_command cmd;
-	memset(&cmd, 0, sizeof(cmd));
+static int ide_read_sector_lba48(drive,buffer,sector)
+int drive;
+void *buffer;
+sector_t sector;
+{
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
+	unsigned int base = read_word(ebda_seg,&EbdaData->controllers[drive>>1].cmd_base);
+	ide_pio_command cmd;
+	memsetb(get_SS(),&cmd, 0, sizeof(cmd));
 	//debug("ide_read_sector_lba48: sector= %ld.\n",(unsigned long) sector);
 
 	cmd.sector_count = 1;
@@ -514,34 +574,37 @@
 	cmd.lba_low2 = (sector >> 24) & 0xff;
 	cmd.lba_mid2 = (sector >> 32) & 0xff;
 	cmd.lba_high2 = (sector >> 40) & 0xff;
-	cmd.device =  info->slave | IDE_DH_LBA;
+	cmd.device =  read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave) | IDE_DH_LBA;
 	cmd.command = IDE_CMD_READ_SECTORS_EXT;
-	return pio_data_in(info->ctrl, &cmd, buffer, IDE_SECTOR_SIZE);
+	return pio_data_in(base, &cmd, buffer, IDE_SECTOR_SIZE);
 }
 
-static inline int ide_read_sector_packet(
-	struct harddisk_info *info, void *buffer, sector_t sector)
+/*Make sure to  pass in a big enough buffer*/
+static int ide_read_sector_packet(drive,buffer,sector)
+int drive;
+void *buffer;
+sector_t sector;
 {
 	char packet[12];
-	static uint8_t cdbuffer[CDROM_SECTOR_SIZE];
-	static struct harddisk_info *last_disk = 0;
+	static int *last_disk = 0;
 	static sector_t last_sector = (sector_t) -1;
 	uint8_t *buf;
 	uint32_t hw_sector;
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
+	unsigned int base = read_word(ebda_seg,&EbdaData->controllers[drive>>1].cmd_base);
 
 	//debug("sector=%Ld\n", sector);
 
-	if (info->hw_sector_size == CDROM_SECTOR_SIZE) {
-		buf = cdbuffer;
+	buf = buffer;
+	if (read_dword(ebda_seg,&EbdaData->harddisk_info[drive].hw_sector_size) == CDROM_SECTOR_SIZE) {
 		hw_sector = sector >> 2;
 	} else {
-		buf = buffer;
 		hw_sector = sector;
 	}
 
-	if (buf==buffer || info != last_disk || hw_sector != last_sector) {
+	if (buf==buffer || drive != last_disk || hw_sector != last_sector) {
 		//debug("hw_sector=%u\n", hw_sector);
-		memset(packet, 0, sizeof packet);
+		memsetb(get_SS(),packet, 0, sizeof packet);
 		packet[0] = 0x28; /* READ */
 		packet[2] = hw_sector >> 24;
 		packet[3] = hw_sector >> 16;
@@ -550,41 +613,46 @@
 		packet[7] = 0;
 		packet[8] = 1; /* length */
 
-		if (pio_packet(info, 1, packet, sizeof packet,
-					buf, info->hw_sector_size) != 0) {
+		if (pio_packet(drive, 1, packet, sizeof packet,
+					buf, read_dword(ebda_seg,&EbdaData->harddisk_info[drive].hw_sector_size)) != 0) {
 			debug("read error\n");
 			return -1;
 		}
-		last_disk = info;
+		last_disk = drive;
 		last_sector = hw_sector;
 	}
 
-	if (buf != buffer)
-		memcpy(buffer, &cdbuffer[(sector & 3) << 9], IDE_SECTOR_SIZE);
 	return 0;
 }
 
-int ide_read(int drive, sector_t sector, void *buffer)
+int ide_read(drive, sector,buffer)
+int drive;
+sector_t sector;
+void *buffer;
 {
-	struct harddisk_info *info = &harddisk_info[drive];
+	   //Myles
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
 	int result;
 
 	//debug("drive=%d, sector=%ld\n",drive,(unsigned long) sector);
 	/* Report the buffer is empty */
-	if (sector > info->sectors) {
+	if (sector > read_dword(ebda_seg,&EbdaData->harddisk_info[drive].sectors)) {
 		return -1;
 	}
-	if (info->address_mode == ADDRESS_MODE_CHS) {
-		result = ide_read_sector_chs(info, buffer, sector);
+	if ( read_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode) == ADDRESS_MODE_CHS) {
+		result = ide_read_sector_chs(drive, buffer, sector);
 	}
-	else if (info->address_mode == ADDRESS_MODE_LBA) {
-		result = ide_read_sector_lba(info, buffer, sector);
+	else if (
+	read_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode) == ADDRESS_MODE_LBA) {
+		result = ide_read_sector_lba(drive, buffer, sector);
 	}
-	else if (info->address_mode == ADDRESS_MODE_LBA48) {
-		result = ide_read_sector_lba48(info, buffer, sector);
+	else if (
+	read_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode) == ADDRESS_MODE_LBA48) {
+		result = ide_read_sector_lba48(drive, buffer, sector);
 	}
-	else if (info->address_mode == ADDRESS_MODE_PACKET) {
-		result = ide_read_sector_packet(info, buffer, sector);
+	else if (
+	read_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode) == ADDRESS_MODE_PACKET) {
+		result = ide_read_sector_packet(drive, buffer, sector);
 	}
 	else {
 		result = -1;
@@ -592,60 +660,64 @@
 	return result;
 }
 
-static int init_drive_x(struct harddisk_info *info, struct controller *ctrl,
-		int slave, int drive, unsigned char *buffer, int ident_command)
+static int init_drive_x(drive,buffer,ident_command)
+int drive;
+unsigned char *buffer;
+int ident_command;
 {
 	uint16_t* drive_info;
-	struct ide_pio_command cmd;
+	ide_pio_command cmd;
 	int i;
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
+	unsigned int base = read_word(ebda_seg,&EbdaData->controllers[drive>>1].cmd_base);
 
-	info->ctrl = ctrl;
-	info->heads = 0u;
-	info->cylinders = 0u;
-	info->sectors_per_track = 0u;
-	info->address_mode = IDE_DH_CHS;
-	info->sectors = 0ul;
-	info->drive_exists = 0;
-	info->slave_absent = 0;
-	info->removable = 0;
-	info->hw_sector_size = IDE_SECTOR_SIZE;
-	info->slave = slave?IDE_DH_SLAVE: IDE_DH_MASTER;
+	write_word(ebda_seg,&EbdaData->harddisk_info[drive].heads,0u);
+	write_word(ebda_seg,&EbdaData->harddisk_info[drive].cylinders,0u);
+	write_word(ebda_seg,&EbdaData->harddisk_info[drive].sectors_per_track,0u);
+	write_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode,IDE_DH_CHS);
+	write_dword(ebda_seg,&EbdaData->harddisk_info[drive].sectors,0ul);
+	write_byte(ebda_seg,&EbdaData->harddisk_info[drive].drive_exists,0);
+	write_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave_absent,0);
+	write_byte(ebda_seg,&EbdaData->harddisk_info[drive].removable,0);
+	write_dword(ebda_seg,&EbdaData->harddisk_info[drive].hw_sector_size,IDE_SECTOR_SIZE);
+	write_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave,(drive&~1)?IDE_DH_SLAVE: IDE_DH_MASTER);
 
-	debug("Testing for hd%c\n", 'a'+drive);
+	debug("Testing for hd%c\n", 'a'+(drive & 1));
 
 	/* Select the drive that we are testing */
-	outb(IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave, 
-		IDE_REG_DEVICE(ctrl));
+	outb_filo(IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave),
+		IDE_REG_DEVICE(base));
 	mdelay(50);
 
 	/* Test to see if the drive registers exist,
 	 * In many cases this quickly rules out a missing drive.
 	 */
 	for(i = 0; i < 4; i++) {
-		outb(0xaa + i, (ctrl->cmd_base) + 2 + i);
+		outb_filo(0xaa + i, (base) + 2 + i);
 	}
 	for(i = 0; i < 4; i++) {
-		if (inb((ctrl->cmd_base) + 2 + i) != 0xaa + i) {
+		if (inb((base) + 2 + i) != 0xaa + i) {
 			return 1;
 		}
 	}
 	for(i = 0; i < 4; i++) {
-		outb(0x55 + i, (ctrl->cmd_base) + 2 + i);
+		outb_filo(0x55 + i, (base) + 2 + i);
 	}
 	for(i = 0; i < 4; i++) {
-		if (inb((ctrl->cmd_base) + 2 + i) != 0x55 + i) {
+		if (inb((base) + 2 + i) != 0x55 + i) {
 			return 1;
 		}
 	}
 	debug("Probing for hd%c\n", 'a'+drive);
 	
-	memset(&cmd, 0, sizeof(cmd));
-	cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave;
+	memsetb(get_SS(),&cmd, 0, sizeof(cmd));
+	cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave);
 	cmd.command = ident_command;
 
 	
-	if (pio_data_in(ctrl, &cmd, buffer, IDE_SECTOR_SIZE) < 0) {
+	if (pio_data_in(base, &cmd, buffer, IDE_SECTOR_SIZE) < 0) {
 		/* Well, if that command didn't work, we probably don't have drive. */
+		 debug("pio_data_in failed for hd%c\n", 'a'+drive);
 		return 1;
 	}
 	
@@ -654,11 +726,12 @@
 	drive_info = (uint16_t *)buffer;
 	if (drive_info[2] == 0x37C8) {
 		/* If the response is incomplete spin up the drive... */
-		memset(&cmd, 0, sizeof(cmd));
+		memsetb(get_SS(),&cmd, 0, sizeof(cmd));
 		cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
-			info->slave;
+               read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave);
 		cmd.feature = IDE_FEATURE_STANDBY_SPINUP_DRIVE;
-		if (pio_non_data(ctrl, &cmd) < 0) {
+		if (pio_non_data(base, &cmd) < 0) {
+			 debug("pio_non_data failed for hd%c\n", 'a'+drive);
 			/* If the command doesn't work give up on the drive */
 			return 1;
 		}
@@ -666,12 +739,13 @@
 	}
 	if ((drive_info[2] == 0x37C8) || (drive_info[2] == 0x8C73)) {
 		/* The response is incomplete retry the drive info command */
-		memset(&cmd, 0, sizeof(cmd));
+		memsetb(get_SS(),&cmd, 0, sizeof(cmd));
 		cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
-			info->slave;
+               read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave);
 		cmd.command = ident_command;
-		if(pio_data_in(ctrl, &cmd, buffer, IDE_SECTOR_SIZE) < 0) {
+		if(pio_data_in(base, &cmd, buffer, IDE_SECTOR_SIZE) < 0) {
 			/* If the command didn't work give up on the drive. */
+			 debug("incomplete retry pio_data_in failed for hd%c\n", 'a'+drive);
 			return 1;
 		}
 	}
@@ -684,58 +758,55 @@
 		return 1;
 	}
 	for(i = 27; i < 47; i++) {
-		info->model_number[((i-27)<< 1)] = (drive_info[i] >> 8) & 0xff;
-		info->model_number[((i-27)<< 1)+1] = drive_info[i] & 0xff;
+	     write_byte(ebda_seg,&EbdaData->harddisk_info[drive].model_number[((i-27)<< 1)],(drive_info[i] >> 8) & 0xff);
+	     write_byte(ebda_seg,&EbdaData->harddisk_info[drive].model_number[((i-27)<< 1)+1], drive_info[i] & 0xff);
 	}
-	info->model_number[40] = '\0';
-	info->drive_exists = 1;
+	write_byte(ebda_seg,&EbdaData->harddisk_info[drive].model_number[40],'\0');
+	write_byte(ebda_seg,&EbdaData->harddisk_info[drive].drive_exists, 1);
 
 	/* See if LBA is supported */
 	if (ident_command == IDE_CMD_IDENTIFY_PACKET_DEVICE) {
-		info->address_mode = ADDRESS_MODE_PACKET;
-		info->removable = 1; /* XXX */
+          write_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode,ADDRESS_MODE_PACKET);
+          write_byte(ebda_seg,&EbdaData->harddisk_info[drive].removable,1); /* XXX */
 	} else if (drive_info[49] & (1 << 9)) {
-		info->address_mode = ADDRESS_MODE_LBA;
-		info->sectors = (drive_info[61] << 16) | (drive_info[60]);
+          write_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode,ADDRESS_MODE_LBA);
+          write_dword(ebda_seg,&EbdaData->harddisk_info[drive].sectors,(drive_info[61] << 16) | (drive_info[60]));
 //		debug("LBA mode, sectors=%Ld\n", info->sectors);
 		/* Enable LBA48 mode if it is present */
 		if (drive_info[83] & (1 <<10)) {
 			/* Should LBA48 depend on LBA? */
-			info->address_mode = ADDRESS_MODE_LBA48;
-			info->sectors = 
-				(((sector_t)drive_info[103]) << 48) |
-				(((sector_t)drive_info[102]) << 32) |
-				(((sector_t)drive_info[101]) << 16) |
-				(((sector_t)drive_info[100]) <<  0);
+               write_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode,ADDRESS_MODE_LBA48);
+               write_dword(ebda_seg,&EbdaData->harddisk_info[drive].sectors, 
+				(((sector_t)drive_info[101]) << 16) | (((sector_t)drive_info[100]) <<  0));
 //			debug("LBA48 mode, sectors=%Ld\n", info->sectors);
 		}
 	} else {
-		info->address_mode = ADDRESS_MODE_CHS;
-		info->heads = drive_info[3];
-		info->cylinders = drive_info[1];
-		info->sectors_per_track = drive_info[6];
-		info->sectors = 
-			info->sectors_per_track *
-			info->heads *
-			info->cylinders;
+          write_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode,ADDRESS_MODE_CHS);
+          write_word(ebda_seg,&EbdaData->harddisk_info[drive].heads , drive_info[3]);
+          write_word(ebda_seg,&EbdaData->harddisk_info[drive].cylinders , drive_info[1]);
+          write_word(ebda_seg,&EbdaData->harddisk_info[drive].sectors_per_track, drive_info[6]);
+          write_dword(ebda_seg,&EbdaData->harddisk_info[drive].sectors , 
+			drive_info[6] * //info->sectors_per_track *
+			drive_info[3] * //info->heads *
+			drive_info[1]);//info->cylinders;
 		debug("CHS mode, sectors_per_track=[%d], heads=[%d], cylinders=[%d]\n",
-			info->sectors_per_track,
-			info->heads,
-			info->cylinders);
-//		debug("sectors=%Ld\n", info->sectors);
+			drive_info[6], //info->sectors_per_track *
+			drive_info[3], //info->heads *
+			drive_info[1]);//info->cylinders;
 	}
 	/* See if we have a slave */
-	if (!info->slave && (((drive_info[93] >> 14) & 3) == 1)) {
-		info->slave_absent = !(drive_info[93] & (1 << 5));
+	if (!read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave) && (((drive_info[93] >> 14) & 3) == 1)) {
+          write_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave_absent , !(drive_info[93] & (1 << 5)));
 	}
 
 	/* See if we need to put the device in CFA power mode 1 */
 	if ((drive_info[160] & ((1 << 15) | (1 << 13)| (1 << 12))) ==
 		((1 << 15) | (1 << 13)| (1 << 12))) {
-		memset(&cmd, 0, sizeof(cmd));
-		cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS | info->slave;
+		memsetb(get_SS(),&cmd, 0, sizeof(cmd));
+		cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(0) | IDE_DH_CHS |
+               read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave);
 		cmd.feature = IDE_FEATURE_CFA_ENABLE_POWER_MODE1;
-		if (pio_non_data(ctrl, &cmd) < 0) {
+		if (pio_non_data(base, &cmd) < 0) {
 			/* If I need to power up the drive, and I can't
 			 * give up.
 			 */
@@ -745,17 +816,17 @@
 	}
 
 	/* Some extra steps for older drives.. */
-	if (info->address_mode != ADDRESS_MODE_PACKET) {
+	if (read_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode) != ADDRESS_MODE_PACKET) {
 		/* Initialize drive parameters
 		 * This is an obsolete command (disappeared as of ATA-6)
 		 * but old drives need it before accessing media. */
-		memset(&cmd, 0, sizeof(cmd));
+		memsetb(get_SS(),&cmd, 0, sizeof(cmd));
 		cmd.device = IDE_DH_DEFAULT | IDE_DH_HEAD(drive_info[3] - 1)
-		    | info->slave;
+		    | read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave);
 		cmd.sector_count = drive_info[6];
 		cmd.command = IDE_CMD_INITIALIZE_DRIVE_PARAMETERS;
 		debug("Init device params... ");
-		if (pio_non_data(ctrl, &cmd) < 0)
+		if (pio_non_data(base, &cmd) < 0)
 			debug("failed (ok for newer drives)\n");
 		else
 			{debug("ok\n");}
@@ -763,20 +834,11 @@
 
 	printf("hd%c: %s",
 		'a'+drive,
-		(info->address_mode==ADDRESS_MODE_CHS) ? "CHS" :
-		(info->address_mode==ADDRESS_MODE_LBA) ? "LBA" :
-		(info->address_mode==ADDRESS_MODE_LBA48) ? "LBA48" :
-		(info->address_mode==ADDRESS_MODE_PACKET) ? "ATAPI" : "???");
-#if 0
-// can not pass compiler	
-	if (info->sectors > (10LL*1000*1000*1000/512))
-		printf(" %uGB", (unsigned) (info->sectors / (1000*1000*1000/512)));
-	else if (info->sectors > (10*1000*1000/512))
-		printf(" %uMB", (unsigned) (info->sectors / (1000*1000/512)));
-	else if (info->sectors > 0)
-		printf(" %uKB", (unsigned) (info->sectors / 2));
-#endif
-	printf(": %s\n", info->model_number);
+		    read_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode)==ADDRESS_MODE_CHS ? "CHS" :
+		    read_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode)==ADDRESS_MODE_LBA ? "LBA" :
+		    read_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode)==ADDRESS_MODE_LBA48 ? "LBA48" :
+		    read_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode)==ADDRESS_MODE_PACKET ? "ATAPI" : "???");
+	//printf(": %s\n", info->model_number);
 
 	return 0;
 }
@@ -785,18 +847,19 @@
  * As Eric mentions, we get stuck when the bus has no drive
  * and floating high. To avoid this, try some heuristics.
  * This is based on a paper on Phoenix website. --ts1 */
-static int ide_bus_floating(struct controller *ctrl)
+static int ide_bus_floating(base)
+unsigned int base;
 {
 	unsigned long timeout;
 	unsigned char status;
 
 	/* Test 1: if status reads 0xff, probably no device is present
 	 * on the bus. Repeat this for 20msec. */
-	timeout = currticks() + 20 * TICKS_PER_SEC / 1000;
+	timeout = 0; // mdelay is 2048 inb
 	status = 0;
 	do {
 		/* Take logical OR to avoid chattering */
-		status |= inb(IDE_REG_STATUS(ctrl));
+		status |= inb(IDE_REG_STATUS(base));
 		/* If it makes 0xff, it's possible to be floating, 
 		 * do test2 to ensure. */
 		if (status == 0xff)
@@ -804,7 +867,8 @@
 		/* If BSY bit is not set, it's harmless to continue probing. */
 		if ((status & IDE_STATUS_BSY) == 0)
 			return 0;
-	} while (currticks() < timeout);
+	   timeout++;
+	} while (timeout <40000);
 	/* Timed out. Logical ORed status didn't make 0xFF.
 	 * We have something there. */
 	return 0;
@@ -814,12 +878,12 @@
 	 * compare. Note that ATA spec inhibits this while BSY is set,
 	 * but for many drives this works. This is a confirmation step anyway.
 	 */
-	outb(0xaa, ctrl->cmd_base + 2);
-	outb(0x55, ctrl->cmd_base + 3);
-	outb(0xff, ctrl->cmd_base + 4);
-	if (inb(ctrl->cmd_base+2) == 0xaa
-			&& inb(ctrl->cmd_base+3) == 0x55
-			&& inb(ctrl->cmd_base+4) == 0xff) {
+	outb_filo(0xaa, base + 2);
+	outb_filo(0x55, base + 3);
+	outb_filo(0xff, base + 4);
+	if (inb(base+2) == 0xaa
+			&& inb(base+3) == 0x55
+			&& inb(base+4) == 0xff) {
 		/* We have some registers there. 
 		 * Though this does not mean it is not a NIC or something... */
 		return 0;
@@ -831,9 +895,12 @@
 	return 1;
 }
 
-static int init_controller(struct controller *ctrl, int drive, unsigned char *buffer) 
+static int init_controller(drive,buffer) 
+int drive;
+unsigned char *buffer; 
 {
-	struct harddisk_info *info;
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
+	unsigned int base = read_word(ebda_seg,&EbdaData->controllers[drive>>1].cmd_base);
 
 	/* Put the drives ide channel in a know state and wait
 	 * for the drives to spinup.  
@@ -863,15 +930,15 @@
 	 * 
 	 */
 #if !BSY_SET_DURING_SPINUP
-	if (await_ide(timeout, ctrl, currticks() + IDE_TIMEOUT) < 0) {
+	if (await_ide(TIMEOUT, base, IDE_TIMEOUT) < 0) {
 		return -1;
 	}
 #endif
 	/* ts1: Try some heuristics to avoid waiting for floating bus */
-	if (ide_bus_floating(ctrl))
+	if (ide_bus_floating(base))
 		return -1;
 
-	if (ide_software_reset(ctrl) < 0) {
+	if (ide_software_reset(base) < 0) {
 		return -1;
 	}
 
@@ -885,35 +952,33 @@
 	 */
 
 	/* Now initialize the individual drives */
-	info = &harddisk_info[drive];
-	init_drive_x(info, ctrl, 0, drive, buffer, IDE_CMD_IDENTIFY_DEVICE);
-	if (!info->drive_exists)
-		init_drive_x(info, ctrl, 0, drive, buffer,
-				IDE_CMD_IDENTIFY_PACKET_DEVICE);
-	if (info->drive_exists && !info->slave_absent) {
+	init_drive_x(drive, buffer, IDE_CMD_IDENTIFY_DEVICE);
+	if (!read_byte(ebda_seg,&EbdaData->harddisk_info[drive].drive_exists))
+		init_drive_x(drive, buffer, IDE_CMD_IDENTIFY_PACKET_DEVICE);
+	if (!read_byte(ebda_seg,&EbdaData->harddisk_info[drive].drive_exists)
+	   && !read_byte(ebda_seg,&EbdaData->harddisk_info[drive].slave_absent)) {
 		drive++;
-		info++;
-		init_drive_x(info, ctrl, 1, drive, buffer,
-				IDE_CMD_IDENTIFY_DEVICE);
-		if (!info->drive_exists)
-			init_drive_x(info, ctrl, 1, drive, buffer,
-					IDE_CMD_IDENTIFY_PACKET_DEVICE);
+		init_drive_x(drive, buffer, IDE_CMD_IDENTIFY_DEVICE);
+	     if (!read_byte(ebda_seg,&EbdaData->harddisk_info[drive].drive_exists))
+			init_drive_x(drive, buffer, IDE_CMD_IDENTIFY_PACKET_DEVICE);
 	}
 
 	return 0;
 }
 
-static int
-atapi_request_sense(struct harddisk_info *info, uint8_t *asc, uint8_t *ascq)
+static int atapi_request_sense(drive,asc,ascq)
+int drive;
+uint8_t *asc;
+uint8_t *ascq;
 {
 	uint8_t packet[12];
 	uint8_t buf[18];
 	int i;
 
-	memset(packet, 0, sizeof packet);
+	memsetb(get_SS(),packet, 0, sizeof packet);
 	packet[0] = 0x03; /* REQUEST SENSE */
 	packet[4] = sizeof buf;
-	if (pio_packet(info, 1, packet, sizeof packet, buf, sizeof buf) != 0)
+	if (pio_packet(drive, 1, packet, sizeof packet, buf, sizeof buf) != 0)
 		return -1;
 
 	for (i = 0; i < sizeof buf; i++)
@@ -927,29 +992,38 @@
 	return 0;
 }
 
-static int atapi_detect_medium(struct harddisk_info *info)
+static int detect_medium(drive)
+int drive;
 {
 	uint8_t packet[12];
 	uint8_t buf[8];
 	uint32_t block_len, sectors;
 	unsigned long timeout;
+	unsigned long time;
 	uint8_t asc, ascq;
 	int in_progress;
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
+	unsigned int base = read_word(ebda_seg,&EbdaData->controllers[drive>>1].cmd_base);
+	if (read_word(ebda_seg,&EbdaData->harddisk_info[drive].address_mode) != ADDRESS_MODE_PACKET) {
+		debug("not implemented for non-ATAPI device\n");
+		return -1;
+	}
 
-	memset(packet, 0, sizeof packet);
+	memsetb(get_SS(),packet, 0, sizeof packet);
 	packet[0] = 0x25; /* READ CAPACITY */
 
 	/* Retry READ CAPACITY for 5 seconds unless MEDIUM NOT PRESENT
 	 * is reported by the drive. If the drive reports "IN PROGRESS",
 	 * 30 seconds is added. */
-	timeout = currticks() + 5*TICKS_PER_SEC;
+	timeout = 50;
+	time = 0;
 	in_progress = 0;
-	while (currticks() < timeout) {
-		if (pio_packet(info, 1, packet, sizeof packet, buf, sizeof buf)
+	while (time < timeout) {
+		if (pio_packet(drive, 1, packet, sizeof packet, buf, sizeof buf)
 				== 0)
 			goto ok;
 
-		if (atapi_request_sense(info, &asc, &ascq) == 0) {
+		if (atapi_request_sense(drive, &asc, &ascq) == 0) {
 			if (asc == 0x3a) { /* MEDIUM NOT PRESENT */
 				debug("Device reports MEDIUM NOT PRESENT\n");
 				return -1;
@@ -957,15 +1031,15 @@
 
 			if (asc == 0x04 && ascq == 0x01 && !in_progress) {
 					/* IN PROGRESS OF BECOMING READY */
-				printf("Waiting for drive to detect "
-						"the medium... ");
+				printf("Waiting for drive to detect medium... ");
 				/* Allow 30 seconds more */
-				timeout = currticks() + 30*TICKS_PER_SEC;
+				timeout = 300;
 				in_progress = 1;
 			}
 		}
 
 		mdelay(100);
+		time += 100;
 	}
 	debug("read capacity failed\n");
 	return -1;
@@ -977,23 +1051,12 @@
 		| (uint32_t) buf[7] << 0;
 	debug("block_len=%u\n", block_len);
 
-	switch (block_len) {
-	case CDROM_SECTOR_SIZE:
-	case IDE_SECTOR_SIZE:
-		/* ok */
-		break;
-	case 0:
-	case 2340:
-	case 2352:
-		/* Some CD drives report weird sector sizes.
-		 * These values are taken from linux/drivers/scsi/sr.c */
-		block_len = CDROM_SECTOR_SIZE;
-		break;
-	default:
+	   if (block_len!= CDROM_SECTOR_SIZE || block_len!= IDE_SECTOR_SIZE)
+	   {
 		debugx("Unsupported sector size %u\n", block_len);
 		return -1;
-	}
-	info->hw_sector_size = block_len;
+	   }
+	write_dword(ebda_seg,&EbdaData->harddisk_info[drive].hw_sector_size , block_len);
 
 	sectors = (uint32_t) buf[0] << 24
 		| (uint32_t) buf[1] << 16
@@ -1001,110 +1064,41 @@
 		| (uint32_t) buf[3] << 0;
 
 	debug("sectors=%u\n", sectors);
-	if (info->hw_sector_size == CDROM_SECTOR_SIZE)
+	if (block_len == CDROM_SECTOR_SIZE)
 		sectors <<= 2; /* # of sectors in 512-byte "soft" sector */
-	if (sectors != info->sectors)
+	if (sectors != read_dword(ebda_seg,&EbdaData->harddisk_info[drive].sectors)) 
 		printf("%dMB medium detected\n", sectors>>(20-9));
-	info->sectors = sectors;
+	write_dword(ebda_seg,&EbdaData->harddisk_info[drive].sectors , sectors);
 	return 0;
 }
 
-static int detect_medium(struct harddisk_info *info)
-{
-	if (info->address_mode == ADDRESS_MODE_PACKET) {
-		if (atapi_detect_medium(info) != 0)
-			return -1;
-	} else {
-		debug("not implemented for non-ATAPI device\n");
-		return -1;
-	}
-	return 0;
-}
-
-static int find_ide_controller_compat(struct controller *ctrl, int index)
+static int find_ide_controller(index)
+int index;
 {
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
+	unsigned short ide_base = 0;
 	if (index >= IDE_MAX_CONTROLLERS)
 		return -1;
-	ctrl->cmd_base  = ide_base[index];
-	ctrl->ctrl_base = ide_base[index] + IDE_REG_EXTENDED_OFFSET;
-	return 0;
-}
-
-#ifdef SUPPORT_PCI
-static int find_ide_controller(struct controller *ctrl, int ctrl_index)
-{
-	int pci_index;
-	struct pci_device *dev;
-	unsigned int mask;
-
-	/* A PCI IDE controller has two channels (pri, sec) */
-	pci_index = ctrl_index >> 1;
-
-	/* Find a IDE storage class device */
-	dev = pci_find_device(-1, -1, 0x0180, -1, pci_index); // S-ATA first.
-	if (!dev) {
-		dev = pci_find_device(-1, -1, 0x0101, -1, pci_index);
-		if (!dev) {
-			debugx("PCI IDE #%d not found\n", pci_index);
-			return -1;
-		}
-	}
-
-	debug("found PCI IDE controller %04x:%04x prog_if=%#x\n",
-			dev->vendor, dev->dev_id, ((dev->class>>8) & 0xff));
-
-	/* See how this controller is configured */
-	mask = (ctrl_index & 1) ? 4 : 1;
-	debug("%s channel: ", (ctrl_index & 1) ? "secodary" : "primary");
-	if ( (((dev->class>>8) & 0xff) & mask) || ((dev->class>>16)!= 0x0101)) { // 0x0180 and other must use native PCI mode	
-		uint32_t x;
-		debug("native PCI mode\n");
-		if ((ctrl_index & 1) == 0) {
-			/* Primary channel */
-			pci_read_config_dword(dev, PCI_BASE_ADDRESS_0,&x);
-			ctrl->cmd_base = x;
-			pci_read_config_dword(dev, PCI_BASE_ADDRESS_1,&x);
-			ctrl->ctrl_base = x;
-		} else {
-			/* Secondary channel */
-			pci_read_config_dword(dev, PCI_BASE_ADDRESS_2,&x);
-			ctrl->cmd_base = x;
-			pci_read_config_dword(dev, PCI_BASE_ADDRESS_3,&x);
-			ctrl->ctrl_base = x;
-		}
-		ctrl->cmd_base &= ~3;
-		ctrl->ctrl_base &= ~3;
-	} else {
-		debug("compatibility mode\n");
-		if (find_ide_controller_compat(ctrl, ctrl_index) != 0)
-			return -1;
-	}
-	debug("cmd_base=%#x ctrl_base=%#x\n", ctrl->cmd_base, ctrl->ctrl_base);
-#if 0
-	debug("cmd+0=%0#x\n", inb(ctrl->cmd_base+0));
-	debug("cmd+1=%0#x\n", inb(ctrl->cmd_base+1));
-	debug("cmd+2=%0#x\n", inb(ctrl->cmd_base+2));
-	debug("cmd+3=%0#x\n", inb(ctrl->cmd_base+3));
-	debug("cmd+4=%0#x\n", inb(ctrl->cmd_base+4));
-	debug("cmd+5=%0#x\n", inb(ctrl->cmd_base+5));
-	debug("cmd+6=%0#x\n", inb(ctrl->cmd_base+6));
-	debug("cmd+7=%0#x\n", inb(ctrl->cmd_base+7));
-	debug("ctrl+0=%0#x\n", inb(ctrl->ctrl_base+0));
-	debug("ctrl+1=%0#x\n", inb(ctrl->ctrl_base+1));
-	debug("ctrl+2=%0#x\n", inb(ctrl->ctrl_base+2));
-	debug("ctrl+3=%0#x\n", inb(ctrl->ctrl_base+3));
-#endif
+	   if (index == 0)
+			 ide_base = IDE_BASE0;
+	   else if (index == 1)
+			 ide_base = IDE_BASE1;
+	   else if (index == 2)
+			 ide_base = IDE_BASE2;
+	   else if (index == 3)
+			 ide_base = IDE_BASE3;
+;
+	write_word(ebda_seg,&EbdaData->controllers[index].cmd_base,ide_base);
 	return 0;
 }
-#else /* !SUPPORT_PCI */
-# define find_ide_controller find_ide_controller_compat
-#endif
 
-int ide_probe(int drive)
+int ide_probe(drive)
+int drive;
 {
-	struct controller *ctrl;
+	unsigned int ebda_seg = read_word(0x0040,0x000E);
 	int ctrl_index;
-	struct harddisk_info *info;
+	   //Myles
+	unsigned char ide_buffer[IDE_SECTOR_SIZE];
 
 	if (drive >= IDE_MAX_DRIVES) {
 		debugx("Unsupported drive number\n");
@@ -1114,26 +1108,25 @@
 	/* A controller has two drives (master, slave) */
 	ctrl_index = drive >> 1;
 
-	ctrl = &controllers[ctrl_index];
-	if (ctrl->cmd_base == 0) {
-		if (find_ide_controller(ctrl, ctrl_index) != 0) {
+	   //Myles
+	if (read_word(ebda_seg,&EbdaData->controllers[ctrl_index].cmd_base) == 0) {
+		if (find_ide_controller(ctrl_index) != 0) {
 			debugx("IDE channel %d not found\n", ctrl_index);
 			return -1;
 		}
-		if (init_controller(ctrl, drive & ~1, ide_buffer) != 0) {
+		if (init_controller(drive, ide_buffer) != 0) {
 			printf("No drive detected on IDE channel %d\n",
 					ctrl_index);
 			return -1;
 		}
 	}
-	info = &harddisk_info[drive];
-	if (!info->drive_exists) {
+	if (!read_byte(ebda_seg,&EbdaData->harddisk_info[drive].drive_exists)) {
 		printf("Drive %d does not exist\n", drive);
 		return -1;
 	}
 
-	if (info->removable) {
-		if (detect_medium(info) != 0) {
+	if (read_byte(ebda_seg,&EbdaData->harddisk_info[drive].removable)) {
+		if (detect_medium(drive) != 0) {
 			printf("Media detection failed\n");
 			return -1;
 		}
@@ -1142,4 +1135,16 @@
 	return 0;
 }
 
+int ide_probe_all()
+{
+	   int drive;
+	   unsigned int ebda_seg = read_word(0x0040,0x000E);
+	   for (drive=0; drive<IDE_MAX_DRIVES; drive++)
+	   {
+		 write_word(ebda_seg,&EbdaData->controllers[drive>>1].cmd_base,0);
+	   }
+	   for (drive=0;drive< IDE_MAX_DRIVES; drive++)
+			 ide_probe(drive);
+	   return 0;
+}
 /* vim:set sts=8 sw=8: */
