/* $Id: writeatatest.c 72 2015-12-20 18:29:17Z bird $ */ /** @file * Does a little write test. */ /* * Copyright (c) 2015 knut st. osmundsen * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with This program. If not, see * */ /********************************************************************************************************************************* * Header Files * *********************************************************************************************************************************/ #include #include #include #include #include #include #include /********************************************************************************************************************************* * Structures and Typedefs * *********************************************************************************************************************************/ typedef struct REGS16 { uint16_t ax; /**< 0 */ uint16_t cx; /**< 2 */ uint16_t dx; /**< 4 */ uint16_t si; /**< 6 */ uint16_t di; /**< 8 */ uint16_t es; /**< 10 */ uint16_t efl; /**< 12 */ uint16_t ds; /**< 14 */ uint16_t bx; /**< 16 */ } REGS16; #define X86_EFL_CF 1 /* The necessary I/O ports, indexed by "bus". */ #define ATA_PORT_SHIFT 1 /* For XT-CF trick */ #define ATA_REG_DATA(x) (x) #define ATA_REG_FEATURES(x) ((x) + (1 << ATA_PORT_SHIFT)) #define ATA_REG_SECTOR_COUNT(x) ((x) + (2 << ATA_PORT_SHIFT)) #define ATA_REG_SECTOR_NUMBER(x) ((x) + (3 << ATA_PORT_SHIFT)) #define ATA_REG_CYLINDER_LOW(x) ((x) + (4 << ATA_PORT_SHIFT)) #define ATA_REG_CYLINDER_HIGH(x) ((x) + (5 << ATA_PORT_SHIFT)) #define ATA_REG_HEAD(x) ((x) + (6 << ATA_PORT_SHIFT)) #define ATA_REG_LBA_0_7(x) ((x) + (3 << ATA_PORT_SHIFT)) #define ATA_REG_LBA_8_15(x) ((x) + (4 << ATA_PORT_SHIFT)) #define ATA_REG_LBA_16_23(x) ((x) + (5 << ATA_PORT_SHIFT)) #define ATA_REG_LBA_24_27_MODE(x) ((x) + (6 << ATA_PORT_SHIFT)) #define ATA_LBA_MODE UINT8_C(0x40) /**< Selects LBA mode in ATA_REG_LBA_24_27_MODE. */ #define ATA_REG_DEVICE_SELECT(x) ((x) + (6 << ATA_PORT_SHIFT)) #define ATA_REG_COMMAND(x) ((x) + (7 << ATA_PORT_SHIFT)) #define ATA_REG_STATUS(x) ATA_REG_COMMAND(x) #define ATA_REG_ALT_STATUS(x) ATA_REG_CONTROL(x) #define ATA_STS_BUSY UINT8_C(0x80) #define ATA_STS_DRDY UINT8_C(0x40) #define ATA_STS_DF UINT8_C(0x20) #define ATA_STS_DSC UINT8_C(0x10) #define ATA_STS_DRQ UINT8_C(0x08) #define ATA_STS_CORR UINT8_C(0x04) #define ATA_STS_IDX UINT8_C(0x02) #define ATA_STS_ERR UINT8_C(0x01) #define ATA_REG_ERROR(x) ATA_REG_FEATURES(x) #define ATA_ERR_RSVR UINT8_C(0x80) #define ATA_ERR_UNC UINT8_C(0x40) #define ATA_ERR_MC UINT8_C(0x20) #define ATA_ERR_IDNF UINT8_C(0x10) #define ATA_ERR_MCR UINT8_C(0x08) #define ATA_ERR_ABRT UINT8_C(0x04) #define ATA_ERR_TKNONF UINT8_C(0x02) #define ATA_ERR_AMNF UINT8_C(0x01) #define ATA_REG_CONTROL(x) ((x) + (14 << ATA_PORT_SHIFT)) #define ATA_CTL_IEN UINT8_C(0x02) /**< Interrupt enable. */ #define ATA_CTL_SRST UINT8_C(0x04) /**< software reset */ #define ATA_CMD_NOP UINT8_C(0x00) #define ATA_CMD_READ_SECTORS UINT8_C(0x20) #define ATA_CMD_READ_SECTORS_NR UINT8_C(0x21) #define ATA_CMD_READ_LONG UINT8_C(0x22) #define ATA_CMD_READ_LONG_NR UINT8_C(0x23) #define ATA_CMD_WRITE_SECTORS UINT8_C(0x30) #define ATA_CMD_WRITE_SECTORS_NR UINT8_C(0x31) #define ATA_CMD_WRITE_LONG UINT8_C(0x32) #define ATA_CMD_WRITE_LONG_NR UINT8_C(0x33) #define ATA_CMD_INIT_DEVICE_PARAMS UINT8_C(0x91) #define ATA_CMD_SET_FEATURES UINT8_C(0xef) #define ATA_CMD_IDENTIFY_DEVICE UINT8_C(0xec) #define ATA_DEV_MASTER UINT8_C(0x00) /**< Master device selection bit value. */ #define ATA_DEV_SLAVE UINT8_C(0x10) /**< Slave device selection bit value. */ #define ATA_FEATURE_EN_8BIT_DATA UINT8_C(0x01) #define ATA_FEATURE_DI_8BIT_DATA UINT8_C(0x81) #define ATA_FEATURE_EN_WRITE_CACHE UINT8_C(0x02) #define ATA_FEATURE_DI_WRITE_CACHE UINT8_C(0x82) #define ATA_FEATURE_SET_XFERMODE UINT8_C(0x03) #define ATA_FV_XFERMODE_PIO_MODE_DEFAULT UINT8_C(0x00) #define ATA_FV_XFERMODE_PIO_MODE_DEFAULT_NO_IORDY UINT8_C(0x01) #define ATA_FV_XFERMODE_PIO_MODE_XXX_FLAG UINT8_C(0x08) #define ATA_FV_XFERMODE_SWDMA_MODE_XXX_FLAG UINT8_C(0x10) #define ATA_FV_XFERMODE_MWDMA_MODE_XXX_FLAG UINT8_C(0x20) /** Delay a bit by reading PIC mask. Should take 4-5 bus cycles, * and thus be more than the required 400ns delay on old computers. */ #define ATA_DELAY_400NS() do { inp(0x21); } while (0) /********************************************************************************************************************************* * Global Variables * *********************************************************************************************************************************/ uint16_t g_uBasePort = 0x300; uint16_t g_uPortShift = 1; uint8_t g_fUseLbaMode = 1; uint8_t g_f8BitData = 1; uint8_t g_bDevice = ATA_DEV_MASTER; uint8_t g_bDrv = 0x80; uint16_t g_cHeads; uint8_t g_cSectorsPerTrack; uint16_t g_cCylinders; uint16_t g_cSectorsPerCylinder; /** The result of the identify command. */ uint16_t g_awIdentify[256]; size_t AtaPrintStatus(FILE *pOut, uint8_t bSts) { size_t cch = fprintf(pOut, "%#x", bSts); if (bSts & ATA_STS_BUSY) cch += fprintf(pOut, " busy"); if (bSts & ATA_STS_DRDY) cch += fprintf(pOut, " drdy"); if (bSts & ATA_STS_DF ) cch += fprintf(pOut, " df"); if (bSts & ATA_STS_DSC ) cch += fprintf(pOut, " dsc"); if (bSts & ATA_STS_DRQ ) cch += fprintf(pOut, " drq"); if (bSts & ATA_STS_CORR) cch += fprintf(pOut, " corr"); if (bSts & ATA_STS_IDX ) cch += fprintf(pOut, " idx"); if (bSts & ATA_STS_ERR ) cch += fprintf(pOut, " err"); return cch; } size_t AtaPrintError(FILE *pOut, uint8_t bErr) { size_t cch = fprintf(pOut, "%#x", bErr); if (bErr & ATA_ERR_RSVR ) cch += fprintf(pOut, " rsrv"); if (bErr & ATA_ERR_UNC ) cch += fprintf(pOut, " unc"); if (bErr & ATA_ERR_MC ) cch += fprintf(pOut, " mc"); if (bErr & ATA_ERR_IDNF ) cch += fprintf(pOut, " idnf"); if (bErr & ATA_ERR_MCR ) cch += fprintf(pOut, " mcr"); if (bErr & ATA_ERR_ABRT ) cch += fprintf(pOut, " abrt"); if (bErr & ATA_ERR_TKNONF) cch += fprintf(pOut, " tknonf"); if (bErr & ATA_ERR_AMNF ) cch += fprintf(pOut, " amnf"); return cch; } static int AtaError(uint8_t bSts, const char *pszFormat, ...) { va_list va; fprintf(stderr, "error: "); va_start(va, pszFormat); vfprintf(stderr, pszFormat, va); va_end(va); fprintf(stderr, "\n status="); AtaPrintStatus(stderr, bSts); fprintf(stderr, "\n error= "); AtaPrintError(stderr, inp(ATA_REG_ERROR(g_uBasePort))); fprintf(stderr, "\n"); return -1; } uint8_t AtaWaitBusy(void) { uint32_t cLoops = 0; uint8_t bStatus; do { if ((++cLoops & 0xfffff) == 0) fprintf(stderr, "AtaWaitBusyForData: cLoops=%#lx\n", cLoops); bStatus = inp(ATA_REG_STATUS(g_uBasePort)); } while ((bStatus & (ATA_STS_BUSY | ATA_STS_ERR)) == ATA_STS_BUSY); return bStatus; } uint8_t AtaWaitBusyDeviceReady(void) { uint32_t cLoops = 0; uint8_t bStatus; do { if ((++cLoops & 0xfffff) == 0) fprintf(stderr, "AtaWaitBusyForData: cLoops=%#lx\n", cLoops); bStatus = inp(ATA_REG_STATUS(g_uBasePort)); } while ( (bStatus & (ATA_STS_BUSY | ATA_STS_DRDY)) != ATA_STS_DRDY && !(bStatus & ATA_STS_ERR) ); return bStatus; } uint8_t AtaWaitBusyForData(void) { uint32_t cLoops = 0; uint8_t bStatus; do { if ((++cLoops & 0xfffff) == 0) fprintf(stderr, "AtaWaitBusyForData: cLoops=%#lx\n", cLoops); bStatus = inp(ATA_REG_STATUS(g_uBasePort)); } while ( (bStatus & (ATA_STS_BUSY | ATA_STS_DRQ)) != ATA_STS_DRQ && !(bStatus & ATA_STS_ERR) ); return bStatus; } uint8_t AtaSubmitCommandAndWait(uint8_t bCommand) { outp(ATA_REG_COMMAND(g_uBasePort), bCommand); ATA_DELAY_400NS(); return AtaWaitBusy(); } uint8_t AtaSubmitCommandAndWaitForData(uint8_t bCommand) { outp(ATA_REG_COMMAND(g_uBasePort), bCommand); ATA_DELAY_400NS(); return AtaWaitBusyForData(); } uint8_t AtaSelectDevice(uint8_t bDevice) { outp(ATA_REG_DEVICE_SELECT(g_uBasePort), g_bDevice); ATA_DELAY_400NS(); return AtaWaitBusyDeviceReady(); } void AtaSetSectorAddress(uint32_t iSector, uint8_t bDevice) { if (g_fUseLbaMode) { outp(ATA_REG_LBA_0_7(g_uBasePort), iSector & 0xff); outp(ATA_REG_LBA_8_15(g_uBasePort), (iSector >> 8) & 0xff); outp(ATA_REG_LBA_16_23(g_uBasePort), (iSector >> 16) & 0xff); outp(ATA_REG_LBA_24_27_MODE(g_uBasePort), ((iSector >> 24) & 0x0f) | ATA_LBA_MODE | bDevice); } else { uint16_t iCyl = iSector / g_cSectorsPerCylinder; uint16_t iRem = iSector % g_cSectorsPerCylinder; uint8_t iHd = iRem / g_cSectorsPerTrack; uint8_t iSec = iRem % g_cSectorsPerTrack; outp(ATA_REG_SECTOR_NUMBER(g_uBasePort), iSec); outp(ATA_REG_CYLINDER_LOW(g_uBasePort), iCyl & 0xff); outp(ATA_REG_CYLINDER_HIGH(g_uBasePort), iCyl >> 8); outp(ATA_REG_HEAD(g_uBasePort), iHd | bDevice); } } void AtaReadData(void *pvBuf, size_t cb, uint8_t f8BitData) { uint16_t uDataPort = ATA_REG_DATA(g_uBasePort); uint16_t *pu16 = (uint16_t *)pvBuf; cb >>= 1; if (f8BitData) { while (cb-- > 0) { uint8_t b1 = inp(uDataPort); uint8_t b2 = inp(uDataPort); *pu16++ = b1 | ((uint16_t)b2 << 8); } } else { while (cb-- > 0) *pu16++ = inpw(uDataPort); } } ////static uint8_t AtaWaitForDataRequest(size_t cbLeft) ////{ //// uint16_t uAltStsPort = ATA_REG_ALT_STATUS(g_uBasePort); //// uint8_t bStsFirst = inp(uAltStsPort); //// uint8_t bSts = bStsFirst; //// uint32_t cLoops = 0; //// do //// { //// if (++cLoops & 0xffff) //// { ////static unsigned x = 0; ////if (x < 16) ////{ //// printf("AtaWaitForDataRequest: bFirst=%#x bLast=%#x cbLeft=%#x\n", bStsFirst, bSts, cbLeft); //// x++; ////} //// break; //// } //// bSts = inp(uAltStsPort); //// } while (!(bSts & (ATA_STS_DRQ | ATA_STS_ERR))); //// return bSts; ////} void xchg(uint8_t volatile *pb); #pragma aux xchg = \ "xchg [si], cx" \ "xchg [si], cx" \ "xchg [si], cx" \ "xchg [si], cx" \ parm [si] \ modify exact [cx]; void AtaWriteData(void const *pvBuf, size_t cb, uint8_t f8BitData) { // uint16_t uAltStsPort = ATA_REG_ALT_STATUS(g_uBasePort); uint16_t register uDataPort = ATA_REG_DATA(g_uBasePort); if (f8BitData) { #if 0 uint8_t const * volatile pbSrc = (uint8_t const *)pvBuf; uint8_t volatile ab[64]; uint8_t volatile *pb = &ab[0]; while (((uintptr_t)pb & 0x1f) != 0x1f) pb++; //uint8_t bSts1, bSts2; inp(0x21); xchg(pb); while (cb-- > 0) { uint8_t b = *pbSrc++; xchg(pb); outp(uDataPort, b); xchg(pb); b = *pbSrc++; xchg(pb); //inp(0x21); outp(uDataPort, b); xchg(pb); //inp(0x21); //if (cb < 30) //{ // if ((cb & 3) == 3) // printf("bSts1=%#x bSts2=%#x ", bSts1, bSts2); // else // printf("bSts1=%#x bSts2=%#x\n", bSts1, bSts2); //} } inp(0x21); #else uint16_t const *pu16 = (uint16_t const *)pvBuf; cb >>= 1; while (cb-- > 0) { uint16_t register u16 = *pu16++; outp(uDataPort, (uint8_t)u16); outp(uDataPort, (uint8_t)(u16 >> 8)); } #endif } else { uint16_t const *pu16 = (uint16_t const *)pvBuf; cb >>= 1; while (cb-- > 0) outp(uDataPort, *pu16++); } } int AtaReadSector(uint32_t iSector, void *pvBuf) { uint8_t bSts = AtaWaitBusy(); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Prepping for reading sector %lu", iSector); printf("AtaReadSector #2\n"); bSts = AtaSelectDevice(g_bDevice); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Selecting device for reading sector %lu", iSector); //printf("AtaReadSector #3\n"); outp(ATA_REG_FEATURES(g_uBasePort), 0x0); outp(ATA_REG_SECTOR_COUNT(g_uBasePort), 1); AtaSetSectorAddress(iSector, g_bDevice); //printf("AtaReadSector #4\n"); bSts = AtaSubmitCommandAndWaitForData(ATA_CMD_READ_SECTORS); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Reading sector %lu", iSector); if (!(bSts & ATA_STS_DRQ)) return AtaError(bSts, "DRQ not set after reading sector %lu", iSector); //printf("AtaReadSector #5\n"); AtaReadData(pvBuf, 512, g_f8BitData); //printf("AtaReadSector #6: bSts=%#x\n", inp(ATA_REG_ALT_STATUS(g_uBasePort))); bSts = inp(ATA_REG_STATUS(g_uBasePort)); if ((bSts & ATA_STS_DRQ)) return AtaError(bSts, "DRQ is still set after reading sector %lu", iSector); if ((bSts & ATA_STS_ERR)) return AtaError(bSts, "ERR is set after reading sector %lu (#2)", iSector); return 0; } int AtaWriteSector(uint32_t iSector, void const *pvBuf) { //int x = printf("AtaWriteSector #1\n"); uint8_t bSts = AtaWaitBusy(); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Prepping for writing sector %lu", iSector); printf("AtaWriteSector #2\n"); bSts = AtaSelectDevice(g_bDevice); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Selecting device for writing sector %lu", iSector); outp(ATA_REG_FEATURES(g_uBasePort), 0x0); outp(ATA_REG_SECTOR_COUNT(g_uBasePort), 1); AtaSetSectorAddress(iSector, g_bDevice); //printf("AtaWriteSector #3\n"); bSts = AtaSubmitCommandAndWaitForData(ATA_CMD_WRITE_SECTORS); if (bSts & ATA_STS_ERR) return AtaError(bSts, "writing sector (#1) %lu", iSector); if (!(bSts & ATA_STS_DRQ)) return AtaError(bSts, "DRQ not set after writing sector (#1) %lu", iSector); //printf("AtaWriteSector #4\n"); AtaWriteData(pvBuf, 512, g_f8BitData); //printf("AtaWriteSector #5\n"); ATA_DELAY_400NS(); bSts = AtaWaitBusy(); //printf("AtaWriteSector #6\n"); if (bSts & ATA_STS_ERR) return AtaError(bSts, "writing sector (#2) %lu", iSector); if (bSts & ATA_STS_DRQ) return AtaError(bSts, "DRQ is set after writing sector (#2) %lu", iSector); return 0; } int AtaIdentifyDevice(uint8_t bDevice, void *pvBuf) { uint8_t bSts = AtaWaitBusy(); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Prepping for device %#x identification", bDevice); bSts = AtaSelectDevice(g_bDevice); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Selecting device %#x for identification", bDevice); outp(ATA_REG_FEATURES(g_uBasePort), 0); outp(ATA_REG_SECTOR_COUNT(g_uBasePort), 0); outp(ATA_REG_SECTOR_NUMBER(g_uBasePort), 0); outp(ATA_REG_CYLINDER_LOW(g_uBasePort), 0); outp(ATA_REG_CYLINDER_HIGH(g_uBasePort), 0); //outp(ATA_REG_HEAD(g_uBasePort), g_bDevice); bSts = AtaSubmitCommandAndWaitForData(ATA_CMD_IDENTIFY_DEVICE); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Device %#x identification", bDevice); if (!(bSts & ATA_STS_DRQ)) return AtaError(bSts, "DRQ not set after device %#x identification", bDevice); AtaReadData(pvBuf, 512, g_f8BitData); return 0; } int AtaSetFeature(uint8_t bDevice, uint8_t bFeature, uint8_t bValue) { uint8_t bSts = AtaWaitBusy(); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Prepping for setting device %#x feature %#x (%#x)", bDevice, bFeature, bValue); bSts = AtaSelectDevice(g_bDevice); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Selecting device %#x for setting feature %#x (%#x)", bDevice, bFeature, bValue); outp(ATA_REG_FEATURES(g_uBasePort), bFeature); outp(ATA_REG_SECTOR_COUNT(g_uBasePort), 0); outp(ATA_REG_SECTOR_NUMBER(g_uBasePort), bValue); outp(ATA_REG_CYLINDER_LOW(g_uBasePort), 0); outp(ATA_REG_CYLINDER_HIGH(g_uBasePort), 0); //outp(ATA_REG_HEAD(g_uBasePort), g_bDevice); bSts = AtaSubmitCommandAndWait(ATA_CMD_SET_FEATURES); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Setting device %#x feature %#x (%#x)", bDevice, bFeature, bValue); if (bSts & ATA_STS_DRQ) return AtaError(bSts, "DRQ is set after setting device %#x feature %#x (%#x)", bDevice, bFeature, bValue); return 0; } int AtaInitDeviceParams(uint8_t bDevice, uint8_t cSectorsPerTrack, uint8_t cHeads) { uint8_t bSts = AtaWaitBusy(); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Prepping for device %#x parameter initialization", bDevice); bSts = AtaSelectDevice(g_bDevice); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Selecting device for device %#x parameter initialization", bDevice); outp(ATA_REG_FEATURES(g_uBasePort), 0); outp(ATA_REG_SECTOR_COUNT(g_uBasePort), cSectorsPerTrack); outp(ATA_REG_SECTOR_NUMBER(g_uBasePort), 0); outp(ATA_REG_CYLINDER_LOW(g_uBasePort), 0); outp(ATA_REG_CYLINDER_HIGH(g_uBasePort), 0); outp(ATA_REG_HEAD(g_uBasePort), g_bDevice | cHeads); bSts = AtaSubmitCommandAndWait(ATA_CMD_INIT_DEVICE_PARAMS); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Device %#x parameter initialization", bDevice); if (bSts & ATA_STS_DRQ) return AtaError(bSts, "DRQ is set after device %#x parameter initialization", bDevice); return 0; } int AtaReset(void) { uint8_t bSts; /* Set the reset flat. */ outp(ATA_REG_CONTROL(g_uBasePort), ATA_CTL_SRST); /* Wait for the busy flag response. */ ATA_DELAY_400NS(); ATA_DELAY_400NS(); while (!(bSts = inp(ATA_REG_STATUS(g_uBasePort))) & ATA_STS_BUSY) ATA_DELAY_400NS(); /* Clear the reset flag. */ outp(ATA_REG_CONTROL(g_uBasePort), 0); ATA_DELAY_400NS(); /* Wait for the controller to become non-busy. */ bSts = AtaWaitBusy(); if (bSts & ATA_STS_ERR) return AtaError(bSts, "Software reset failed"); return 0; } int AtaInit(void) { uint8_t bSts = inp(ATA_REG_ALT_STATUS(g_uBasePort)); printf("alt status="); AtaPrintStatus(stdout, bSts); printf("\n"); bSts = inp(ATA_REG_STATUS(g_uBasePort)); printf(" status="); AtaPrintStatus(stdout, bSts); printf("\n"); if (AtaReset() != 0) return -1; /* Enable 8-bit data transfers (just to be on the safe side). */ AtaSetFeature(g_bDevice, ATA_FEATURE_EN_8BIT_DATA, 0); /* Identify the device. */ memset(g_awIdentify, 0, sizeof(g_awIdentify)); if (AtaIdentifyDevice(g_bDevice, g_awIdentify) != 0) return -1; /** @todo this is rather simple... */ g_cCylinders = g_awIdentify[1]; g_cHeads = g_awIdentify[3]; g_cSectorsPerTrack = g_awIdentify[6]; g_cSectorsPerCylinder = g_cHeads * g_cSectorsPerTrack; printf("Device %#x parameters: %u cylinders, %u heads, %u sectors\n", g_bDevice, g_cCylinders, g_cHeads, g_cSectorsPerTrack); /* Disable stuff and try select pio modes. */ AtaSetFeature(g_bDevice, ATA_FEATURE_DI_WRITE_CACHE, 0); AtaSetFeature(g_bDevice, ATA_FEATURE_SET_XFERMODE, ATA_FV_XFERMODE_PIO_MODE_DEFAULT_NO_IORDY); AtaSetFeature(g_bDevice, ATA_FEATURE_SET_XFERMODE, ATA_FV_XFERMODE_PIO_MODE_XXX_FLAG | 0); return 0; } /* * INT13h access methods * INT13h access methods * INT13h access methods */ void BiosCall13(REGS16 *pRegs); #pragma aux BiosCall13 = \ "push ax" \ "push cx" \ "push dx" \ "push bp" \ "push si" \ "push di" \ "push es" \ "push bx" \ "push ds" \ \ "mov ax, [bx]" \ "mov cx, [bx + 2]" \ "mov dx, [bx + 4]" \ "mov si, [bx + 6]" \ "mov di, [bx + 8]" \ "mov es, [bx + 10]" \ "push word ptr [bx + 12]" \ "popf" \ "push word ptr [bx + 14]" \ "push word ptr [bx + 16]" \ "pop bx" \ "pop ds" \ \ "int 13h"\ \ "push ds" \ "push bx" \ "mov bp, sp" \ "mov ds, [bp + 4]" \ "mov bx, [bp + 6]" \ "mov [bx], ax" \ "mov [bx + 2], cx" \ "mov [bx + 4], dx" \ "mov [bx + 6], si" \ "mov [bx + 8], di" \ "mov [bx + 10], es" \ "pushf" \ "pop ax" \ "mov [bx + 12], ax" \ "pop ax" \ "mov [bx + 14], ax" \ "pop ax" \ "mov [bx + 16], ax" \ \ "pop ds" \ "pop bx" \ "pop es" \ "pop di" \ "pop si" \ "pop bp" \ "pop dx" \ "pop cx" \ "pop ax" \ parm [bx]; int Int13hInit(void) { REGS16 Regs; memset(&Regs, 0, sizeof(Regs)); Regs.ax = 0x0800; Regs.dx = g_bDrv; BiosCall13(&Regs); /** @todo check for errors. */ g_cHeads = (Regs.dx >> 8) + 1; g_cSectorsPerTrack = Regs.cx & 0x3f; g_cCylinders = (Regs.cx >> 8) | ((Regs.cx & 0xc0) << 2); g_cSectorsPerCylinder = g_cHeads * g_cSectorsPerTrack; printf("Drive %#x parameters: %u cylinders, %u heads, %u sectors\n", g_bDrv, g_cCylinders, g_cHeads, g_cSectorsPerTrack); if (!(Regs.efl & X86_EFL_CF)) return 0; fprintf(stderr, "Error getting disk params: %#x\n", Regs.ax); return -1; } void SectorNoToInt13(uint32_t iSector, REGS16 *pRegs) { uint16_t iRem = iSector % g_cSectorsPerCylinder; uint16_t iCyl = iSector / g_cSectorsPerCylinder; pRegs->cx = iCyl << 8; pRegs->cx |= (iCyl >> 2) & 0xc0; pRegs->cx |= (iRem % g_cSectorsPerTrack) & 0x3f; pRegs->dx &= UINT16_C(0x00ff); pRegs->dx |= (iRem / g_cSectorsPerTrack) << 8; } int Int13hReadSector(uint32_t iSector, void *pvBuf) { REGS16 Regs; memset(&Regs, 0, sizeof(Regs)); Regs.ax = 0x0201; Regs.dx = g_bDrv; Regs.bx = (unsigned)(void __near *)pvBuf; Regs.es = (__segment)pvBuf; SectorNoToInt13(iSector, &Regs); printf("ax=%#x dx=%#x cx=%#x es:bx=%04x:%04x\n", Regs.ax, Regs.dx, Regs.cx, Regs.es, Regs.bx); BiosCall13(&Regs); if (!(Regs.efl & X86_EFL_CF)) return 0; fprintf(stderr, "Error reading sector %lu on %#x: %#x\n", iSector, g_bDrv, Regs.ax); return -1; } int Int13hWriteSector(uint32_t iSector, void const *pvBuf) { REGS16 Regs; memset(&Regs, 0, sizeof(Regs)); Regs.ax = 0x0301; Regs.dx = g_bDrv; Regs.bx = (unsigned)(void const __near *)pvBuf; Regs.es = (__segment)pvBuf; SectorNoToInt13(iSector, &Regs); printf("ax=%#x dx=%#x cx=%#x es:bx=%04x:%04x\n", Regs.ax, Regs.dx, Regs.cx, Regs.es, Regs.bx); BiosCall13(&Regs); if (!(Regs.efl & X86_EFL_CF)) return 0; fprintf(stderr, "Error writing sector %lu on %#x: %#x\n", iSector, g_bDrv, Regs.ax); return -1; } int GetDriveParams(void) { #ifdef USE_INT13H return Int13hInit(); #else return AtaInit(); #endif } int ReadSector(uint32_t iSector, void *pvBuf) { #ifdef USE_INT13H return Int13hReadSector(iSector, pvBuf); #else return AtaReadSector(iSector, pvBuf); #endif } int WriteSector(uint32_t iSector, void const *pvBuf) { #ifdef USE_INT13H return Int13hWriteSector(iSector, pvBuf); #else return AtaWriteSector(iSector, pvBuf); #endif } static int usage(void) { printf("usage: writetst [sector] [drv]\n"); return 1; } int main(int argc, char **argv) { int rc = 1; /* * Parse parameters. */ uint32_t iSector = 3; g_bDrv = 0x80; g_bDevice = ATA_DEV_MASTER; if (argc > 3) { fprintf(stderr, "too many parameters!\n"); return usage(); } if (argc > 1) { iSector = strtoul(argv[1], NULL, 0); if ( iSector == 0 || (iSector >= 32 && iSector < 65535) || iSector > 0x800000 /*4G*/) { fprintf(stderr, "error: start sector is out of bounds: %s (%lu)\n", argv[1], iSector); return usage(); } } if (argc > 2) { unsigned long uTmp = strtoul(argv[2], NULL, 0); if (uTmp < 0x80 || uTmp > 0x8f) { fprintf(stderr, "error: drive number is out of bounds: %s (%lu)\n", argv[1], uTmp); return usage(); } g_bDrv = (uint8_t)uTmp; g_bDevice = g_bDrv == 0x80 ? ATA_DEV_MASTER : ATA_DEV_SLAVE; /* simplified */ } /* * Detect drive parameters. */ if (GetDriveParams() == 0) { static uint8_t s_abSaved[512]; if (ReadSector(iSector, s_abSaved) == 0) { static uint8_t s_abWrite[512]; unsigned i; unsigned cTries; //unsigned cMaxTries = 20; unsigned cMaxTries = 1; for (i = 0; i < 512; i++) s_abWrite[i] = (uint8_t)i; for (cTries = 0; cTries < cMaxTries && rc != 0; cTries++) { if (WriteSector(iSector, s_abWrite) == 0) { static uint8_t s_abReadBack[512]; if (ReadSector(iSector, s_abReadBack) == 0) { for (i = 0; i < 512; i++) s_abWrite[i] = (uint8_t)i; if (memcmp(s_abReadBack, s_abWrite, sizeof(s_abReadBack)) == 0) { rc = 0; printf("wrote sector and successfully read it back\n"); } else if (cTries >= cMaxTries - 1) { unsigned cErrors = 0; fprintf(stderr, "read back doesn't match what was written:\n"); for (i = 0; i < 512; i++) if (s_abReadBack[i] != (uint8_t)i) { fprintf(stderr, " %03x: %02x->%02x", i, (uint8_t)i, s_abReadBack[i]); if ((cErrors % 5) == 4) fprintf(stderr, "\n"); cErrors++; if (cErrors > 5 * 10) break; } if ((cErrors % 5) != 0) fprintf(stderr, "\n"); } } } } /* restore */ WriteSector(iSector, s_abSaved); } } return rc; }