From 6508de03fdee48e74bc27b0fb3a3bb29c3fa6827 Mon Sep 17 00:00:00 2001 From: Michael McMaster Date: Wed, 11 Sep 2013 14:40:10 +1000 Subject: [PATCH] Compiles with some warnings now. --- DoReadWrite.c | 126 ---- DoSCSICommandWithSense.c | 576 --------------- DoTestUnitReady.c | 100 --- MacSCSICommand.h | 420 ----------- Makefile | 19 +- SCSIStuff.h | 149 ---- SCSI_misc.c | 39 - bitfield.c | 53 +- bitfield.h | 29 +- convert.c | 247 +++---- convert.h | 29 +- dpme.h | 145 ++-- dump.c | 8 +- errors.c | 2 - fdisk.c | 1514 -------------------------------------- fdisk.h | 51 -- fdisklabel.c | 800 -------------------- fdisklabel.h | 245 ------ io.c | 2 +- partition_map.c | 23 +- partition_map.h | 24 +- pdisk.c | 109 +-- 22 files changed, 205 insertions(+), 4505 deletions(-) delete mode 100644 DoReadWrite.c delete mode 100644 DoSCSICommandWithSense.c delete mode 100644 DoTestUnitReady.c delete mode 100644 MacSCSICommand.h delete mode 100644 SCSIStuff.h delete mode 100644 SCSI_misc.c delete mode 100644 fdisk.c delete mode 100644 fdisk.h delete mode 100644 fdisklabel.c delete mode 100644 fdisklabel.h diff --git a/DoReadWrite.c b/DoReadWrite.c deleted file mode 100644 index d3a0c56..0000000 --- a/DoReadWrite.c +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 1993-97 by Apple Computer, Inc. - * All Rights Reserved - * - * Permission to use, copy, modify, and distribute this software and - * its documentation for any purpose and without fee is hereby granted, - * provided that the above copyright notice appears in all copies and - * that both the copyright notice and this permission notice appear in - * supporting documentation. - * - * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE - * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR - * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, - * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION - * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ -#include "SCSIStuff.h" -int DoRead(UInt8 targetID, UInt32 block, UInt16 count, char* addr); -int DoWrite(UInt8 targetID, UInt32 block, UInt16 count, char* addr); - -int -DoRead( - UInt8 targetID, - UInt32 block, - UInt16 count, - char * addr - ) -{ - OSErr status; - Str255 errorText; - char* msg; - static SCSI_10_Byte_Command gReadCommand = { - kScsiCmdRead10, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; - SCSI_Sense_Data senseData; - DeviceIdent scsiDevice; - int rtn_value; - - scsiDevice.diReserved = 0; - scsiDevice.bus = kOriginalSCSIBusAdaptor; - scsiDevice.targetID = targetID; - scsiDevice.LUN = 0; - - gReadCommand.lbn4 = (block >> 24) & 0xFF; - gReadCommand.lbn3 = (block >> 16) & 0xFF; - gReadCommand.lbn2 = (block >> 8) & 0xFF; - gReadCommand.lbn1 = block & 0xFF; - - gReadCommand.len2 = (count >> 8) & 0xFF; - gReadCommand.len1 = count & 0xFF; - - status = DoSCSICommand( - scsiDevice, - "\pRead", - (SCSI_CommandPtr) &gReadCommand, - (Ptr) addr, - count * 512, - scsiDirectionIn, - NULL, - &senseData, - errorText - ); - if (status == noErr) { - rtn_value = 1; - } else { - rtn_value = 0; - } - return rtn_value; -} - -int -DoWrite( - UInt8 targetID, - UInt32 block, - UInt16 count, - char * addr - ) -{ - OSErr status; - Str255 errorText; - char* msg; - static SCSI_10_Byte_Command gWriteCommand = { - kScsiCmdWrite10, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; - SCSI_Sense_Data senseData; - DeviceIdent scsiDevice; - int rtn_value; - - scsiDevice.diReserved = 0; - scsiDevice.bus = 0xff; - scsiDevice.targetID = targetID; - scsiDevice.LUN = 0; - - gWriteCommand.lbn4 = (block >> 24) & 0xFF; - gWriteCommand.lbn3 = (block >> 16) & 0xFF; - gWriteCommand.lbn2 = (block >> 8) & 0xFF; - gWriteCommand.lbn1 = block & 0xFF; - - gWriteCommand.len2 = (count >> 8) & 0xFF; - gWriteCommand.len1 = count & 0xFF; - - status = DoSCSICommand( - scsiDevice, - "\pWrite", - (SCSI_CommandPtr) &gWriteCommand, - (Ptr) addr, - count * 512, - scsiDirectionOut, - NULL, - &senseData, - errorText - ); - if (status == noErr) { - rtn_value = 1; - } else { - rtn_value = 0; - } - return rtn_value; -} - - - diff --git a/DoSCSICommandWithSense.c b/DoSCSICommandWithSense.c deleted file mode 100644 index 0a27e0f..0000000 --- a/DoSCSICommandWithSense.c +++ /dev/null @@ -1,576 +0,0 @@ -/* - * DoScsiCommand.c - * - * This is the common entry to the original and asynchronous SCSI Manager calls: - * if the asynchronous SCSI Manager is requested, it calls it. Otherwise, it - * calls the original SCSI Manager and executes Request Sense if necessary. - * - * This function returns "autosense" in the SCSI_Sense_Data area. This will - * be formatted in the senseMessage string. - */ - -/* - * Copyright 1992, 1993, 1997 by Apple Computer, Inc. - * All Rights Reserved - * - * Permission to use, copy, modify, and distribute this software and - * its documentation for any purpose and without fee is hereby granted, - * provided that the above copyright notice appears in all copies and - * that both the copyright notice and this permission notice appear in - * supporting documentation. - * - * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE - * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR - * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, - * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION - * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ -#include "SCSIStuff.h" - -OSErr OriginalSCSI( - DeviceIdent scsiDevice, - const SCSI_CommandPtr scsiCommand, - UInt8 scsiCommandLen, - Ptr dataBuffer, - ByteCount dataLength, - UInt32 scsiFlags, - ByteCount *actualTransferCount, - UInt8 *scsiStatusByte - ); -UInt16 GetCommandLength( - const SCSI_CommandPtr cmdPtr - ); -/* - * This is the maximum number of times we try to grab the SCSI Bus - */ -#define kMaxSCSIRetries 40 /* 10 seconds, 4 times/sec */ -#define kSCSICommandTimeout (5 * 1000L) /* Five seconds */ - -/* - * This test is TRUE if the SCSI bus status indicates "busy" (which is the case - * if either the BSY or SEL bit is set). - */ -#ifndef kScsiStatBSY -#define kScsiStatBSY (1 << 6) -#endif -#ifndef kScsiStatSEL -#define kScsiStatSEL (1 << 1) -#endif -#define ScsiBusBusy() ((SCSIStat() & (kScsiStatBSY | kScsiStatSEL)) != 0) -Boolean IsVirtualMemoryRunning(void); - -/* - * This returns TRUE if the command failed with "Illegal Request." We need this - * so we can ignore LogSense or ReadDefectData if the device doesn't support - * these functions. - */ -Boolean -IsIllegalRequest( - OSErr scsiStatus, - const SCSI_Sense_Data *senseDataPtr - ) -{ - Boolean result; -#define SENSE (*senseDataPtr) - - result = FALSE; - if (scsiStatus == scsiNonZeroStatus - && (SENSE.senseKey & kScsiSenseKeyMask) == kScsiSenseIllegalReq - && SENSE.additionalSenseLength >= 4) { - switch ((SENSE.additionalSenseCode << 8) | SENSE.additionalSenseQualifier) { - case 0x0000: - case 0x2000: - case 0x2022: /* Obsolete */ - result = TRUE; - break; - default: - break; - } - } - return (result); -#undef SENSE -} - -/* - * This returns TRUE if the command failed with Device Not Ready (No Media Present) - */ -Boolean -IsNoMedia( - OSErr scsiStatus, - const SCSI_Sense_Data *senseDataPtr - ) -{ - Boolean result; -#define SENSE (*senseDataPtr) - - result = FALSE; - if (scsiStatus == scsiNonZeroStatus - && (SENSE.senseKey & kScsiSenseKeyMask) == kScsiSenseNotReady - && SENSE.additionalSenseLength >= 4) { - switch ((SENSE.additionalSenseCode << 8) | SENSE.additionalSenseQualifier) { - case 0x0000: - case 0x3A00: - result = TRUE; - break; - default: - break; - } - } - return (result); -#undef SENSE -} - -OSErr -DoOriginalSCSICommand( - DeviceIdent scsiDevice, - const SCSI_CommandPtr theSCSICommand, - unsigned short cmdBlockLength, - Ptr dataBuffer, - ByteCount dataLength, - UInt32 scsiFlags, - ByteCount *actualTransferCount, - SCSI_Sense_Data *sensePtr - ); - -/* - * Do one SCSI Command. If the device returns Check Condition, issue Request Sense - * (original SCSI Manager only) and interpret the sense data. The original SCSI - * command status is in SCB.status. If it is statusErr or scsiNonZeroStatus, - * the sense data is in SCB.sense and the Request Sense status is in - * SCB.requestSenseStatus. - * - * If sensePtr[0] is non-zero, there is a message. - */ -OSErr -DoSCSICommand( - DeviceIdent scsiDevice, - ConstStr255Param currentAction, - const SCSI_CommandPtr callerSCSICommand, - Ptr dataBuffer, - ByteCount dataLength, - UInt32 scsiFlags, - ByteCount *actualTransferCount, - SCSI_Sense_Data *sensePtr, - StringPtr senseMessage - ) -{ - OSErr status; - SCSI_Command theSCSICommand; - unsigned short cmdBlockLength; - -// SpinSpinner(&gCurrentInfoPtr->spinnerRecord); -// ShowProgressAction(currentAction); - /* - * Store the LUN information in the command block - this is needed - * for devices that only examine the command block for LUN values. - * (On SCSI-II, the asynchronous SCSI Manager also includes the - * LUN in the identify message). - */ - theSCSICommand = *callerSCSICommand; - theSCSICommand.scsi[1] &= ~0xE0; - theSCSICommand.scsi[1] |= (scsiDevice.LUN & 0x03) << 5; - cmdBlockLength = GetCommandLength(&theSCSICommand); - if (senseMessage != NULL) - senseMessage[0] = 0; - if (sensePtr != NULL) - sensePtr->errorCode = 0; - if (scsiDevice.bus == kOriginalSCSIBusAdaptor) { - status = DoOriginalSCSICommand( - scsiDevice, - &theSCSICommand, - cmdBlockLength, - dataBuffer, - dataLength, - scsiFlags, - actualTransferCount, - sensePtr - ); - } - else { - ClearMemory(gSCSIExecIOPBPtr, gSCSIExecIOPBPtrLen); -#define PB (*gSCSIExecIOPBPtr) - PB.scsiPBLength = gSCSIExecIOPBPtrLen; - PB.scsiFunctionCode = SCSIExecIO; - PB.scsiDevice = scsiDevice; - PB.scsiTimeout = kSCSICommandTimeout; - /* - * Fiddle the flags so they're the least disruptive possible. - */ - PB.scsiFlags = scsiFlags | (scsiSIMQNoFreeze | scsiDontDisconnect); - if (sensePtr != NULL) { - PB.scsiSensePtr = (UInt8 *) sensePtr; - PB.scsiSenseLength = sizeof *sensePtr; - } - BlockMoveData(&theSCSICommand, &PB.scsiCDB.cdbBytes[0], cmdBlockLength); - PB.scsiCDBLength = cmdBlockLength; - if (dataBuffer != NULL) { - PB.scsiDataPtr = (UInt8 *) dataBuffer; - PB.scsiDataLength = dataLength; - PB.scsiDataType = scsiDataBuffer; - PB.scsiTransferType = scsiTransferPolled; - } - status = SCSIAction((SCSI_PB *) &PB); - if (status == noErr) - status = PB.scsiResult; - if (status == scsiSelectTimeout) - status = scsiDeviceNotThere; - if (actualTransferCount != NULL) { - /* - * Make sure that the actual transfer count does not exceed - * the allocation count (some devices spit extra data at us!) - */ - *actualTransferCount = dataLength - PB.scsiDataResidual; - if (*actualTransferCount > dataLength) - *actualTransferCount = dataLength; - } -#undef PB - } - if (status == scsiNonZeroStatus - && sensePtr != NULL - && sensePtr->errorCode != 0 - && senseMessage != NULL) { -// FormatSenseMessage(sensePtr, senseMessage); -// ShowProgressAction(senseMessage); - } - return (status); -} - -/* - * Do a command with autosense using the original SCSI manager. - */ -OSErr -DoOriginalSCSICommand( - DeviceIdent scsiDevice, - const SCSI_CommandPtr theSCSICommand, - unsigned short cmdBlockLength, - Ptr dataBuffer, - ByteCount dataLength, - UInt32 scsiFlags, - ByteCount *actualTransferCount, - SCSI_Sense_Data *sensePtr - ) -{ - OSErr status; - UInt8 scsiStatusByte; - SCSI_Command scsiStatusCommand; - - status = OriginalSCSI( - scsiDevice, - theSCSICommand, - cmdBlockLength, - dataBuffer, - dataLength, - scsiFlags, - actualTransferCount, - &scsiStatusByte - ); - if (status == scsiNonZeroStatus - && scsiStatusByte == kScsiStatusCheckCondition - && sensePtr != NULL) { - CLEAR(scsiStatusCommand); - CLEAR(*sensePtr); - scsiStatusCommand.scsi6.opcode = kScsiCmdRequestSense; - scsiStatusCommand.scsi[1] |= (scsiDevice.LUN & 0x03) << 5; - scsiStatusCommand.scsi6.len = sizeof *sensePtr; - status = OriginalSCSI( - scsiDevice, - &scsiStatusCommand, - sizeof scsiStatusCommand.scsi6, - (Ptr) sensePtr, - sizeof *sensePtr, - scsiDirectionIn, - NULL, - &scsiStatusByte - ); - if (status != noErr && status != scsiDataRunError) { -#ifdef notdef - if (gDebugOnError && scsiStatusByte != kScsiStatusCheckCondition) { - Str255 work; - - pstrcpy(work, "\pAutosense failed "); - AppendSigned(work, status); - AppendChar(work, ' '); - AppendHexLeadingZeros(work, scsiStatusByte, 2); - DebugStr(work); - } -#endif - sensePtr->errorCode = 0; - status = scsiAutosenseFailed; - } - else { - status = scsiNonZeroStatus; - } - } - return (status); -} - -OSErr -OriginalSCSI( - DeviceIdent scsiDevice, - const SCSI_CommandPtr scsiCommand, - UInt8 scsiCommandLen, - Ptr dataBuffer, - ByteCount dataLength, - UInt32 scsiFlags, - ByteCount *actualTransferCount, - UInt8 *scsiStatusBytePtr - ) -{ - OSErr status; /* Final status */ - OSErr completionStatus; /* Status from ScsiComplete */ - short totalTries; /* Get/Select retries */ - short getTries; /* Get retries */ - short iCount; /* Bus free counter */ - unsigned long watchdog; /* Timeout after this */ - unsigned long myTransferCount; /* Gets TIB loop counter */ - short scsiStatusByte; /* Gets SCSIComplete result */ - short scsiMsgByte; /* Gets SCSIComplete result */ - Boolean bufferHoldFlag; - /* - * The TIB has the following format: - * [0] scInc user buffer transferQuantum or transferSize - * [1] scAdd &theTransferCount 1 - * [2] scLoop -> tib[0] transferSize / transferQuantum - * [3] scStop - * The intent of this is to return, in actualTransferCount, the number - * of times we cycled through the tib[] loop. This will be the actual - * transfer count if transferQuantum equals one, or the number of - * "blocks" if transferQuantum is the length of one sector. - */ - SCSIInstr tib[4]; /* Current TIB */ - - status = noErr; - bufferHoldFlag = FALSE; - scsiStatusByte = 0xFF; - scsiMsgByte = 0xFF; - myTransferCount = 0; - /* - * If there is a data transfer, setup the tib. - */ - if (dataBuffer != NULL) { - tib[0].scOpcode = scInc; - tib[0].scParam1 = (unsigned long) dataBuffer; - tib[0].scParam2 = 1; - tib[1].scOpcode = scAdd; - tib[1].scParam1 = (unsigned long) &myTransferCount; - tib[1].scParam2 = 1; - tib[2].scOpcode = scLoop; - tib[2].scParam1 = (-2 * sizeof (SCSIInstr)); - tib[2].scParam2 = dataLength / tib[0].scParam2; - tib[3].scOpcode = scStop; - tib[3].scParam1 = 0; - tib[3].scParam2 = 0; - } - if (IsVirtualMemoryRunning() && dataBuffer != NULL) { - /* - * Lock down the user buffer, if any. In a real-world application - * or driver, this would be done before calling the SCSI interface. - */ -#ifdef notdef - FailOSErr( - HoldMemory(dataBuffer, dataLength), - "\pCan't lock data buffer in physical memory" - ); -#else - HoldMemory(dataBuffer, dataLength); -#endif - bufferHoldFlag = TRUE; - } - /* - * Arbitrate for the scsi bus. This will fail if some other device is - * accessing the bus at this time (which is unlikely). - * - *** Do not set breakpoints or call any functions that may require device - *** I/O (such as display code that accesses font resources between - *** SCSIGet and SCSIComplete, - * - */ - for (totalTries = 0; totalTries < kMaxSCSIRetries; totalTries++) { - for (getTries = 0; getTries < 4; getTries++) { - /* - * Wait for the bus to go free. - */ - watchdog = TickCount() + 300; /* 5 second timeout */ - while (ScsiBusBusy()) { - if (/*gStopNow || StopNow() ||*/ TickCount() > watchdog) { - status = scsiBusy; - goto exit; - } - } - /* - * The bus is free, try to grab it - */ - for (iCount = 0; iCount < 4; iCount++) { - if ((status = SCSIGet()) == noErr) - break; - } - if (status == noErr) - break; /* Success: we have the bus */ - /* - * The bus became busy again. Try to wait for it to go free. - */ - for (iCount = 0; - /*gStopNow == FALSE && StopNow() == FALSE &&*/ iCount < 100 && ScsiBusBusy(); - iCount++) - ; - } /* The getTries loop */ - if (status != noErr) { - /* - * The SCSI Manager thinks the bus is not busy and not selected, - * but "someone" has set its internal semaphore that signals - * that the SCSI Manager itself is busy. The application will have - * to handle this problem. (We tried getTries * 4 times). - */ - status = scsiBusy; - goto exit; - } - /* - * We now own the SCSI bus. Try to select the device. - */ - if ((status = SCSISelect(scsiDevice.targetID)) != noErr) { - switch (status) { - /* - * We get scBadParmsErr if we try to arbitrate for the initiator. - */ - case scBadParmsErr: status = scsiTIDInvalid; break; - case scCommErr: status = scsiDeviceNotThere; break; - case scArbNBErr: status = scsiBusy; break; - case scSequenceErr: status = scsiRequestInvalid; break; - } - goto exit; - } - /* - * From this point on, we must exit through SCSIComplete() even if an - * error is detected. Send a command to the selected device. There are - * several failure modes, including an illegal command (such as a - * write to a read-only device). If the command failed because of - * "device busy", we will try it again. - */ - status = SCSICmd((Ptr) scsiCommand, scsiCommandLen); - if (status != noErr) { - switch (status) { - case scCommErr: status = scsiCommandTimeout; break; - case scPhaseErr: status = scsiSequenceFailed; break; - } - } - if (status == noErr && dataBuffer != NULL) { - /* - * This command requires a data transfer. - */ - if (scsiFlags == scsiDirectionOut) - status = SCSIWrite((Ptr) tib); - else { - status = SCSIRead((Ptr) tib); - } - switch (status) { - case scCommErr: status = scsiCommandTimeout; break; - case scBadParmsErr: status = scsiRequestInvalid; break; - case scPhaseErr: status = noErr; /* Don't care */ break; - case scCompareErr: /* Can't happen */ break; - } - } - /* - * SCSIComplete "runs" the bus-phase algorithm until the bitter end, - * returning the status and command-completion message bytes.. - */ - completionStatus = SCSIComplete( - &scsiStatusByte, - &scsiMsgByte, - 5 * 60L - ); - if (status == noErr && completionStatus != noErr) { - switch (completionStatus) { - case scCommErr: status = scsiCommandTimeout; break; - case scPhaseErr: status = scsiSequenceFailed; break; - case scComplPhaseErr: status = scsiSequenceFailed; break; - } - } - if (completionStatus == noErr && scsiStatusByte == kScsiStatusBusy) { - /* - * ScsiComplete is happy. If the device is busy, - * pause for 1/4 second and try again. - */ - watchdog = TickCount() + 15; - while (TickCount() < watchdog) - ; - continue; /* Do next totalTries attempt */ - } - /* - * This is the normal exit (success) or final failure exit. - */ - break; - } /* totalTries loop */ -exit: if (bufferHoldFlag) - (void) UnholdMemory(dataBuffer, dataLength); - /* - * Return the number of bytes transferred to the caller. If the caller - * supplied an actual count and the count is no greater than the maximum, - * ignore any phase errors. - */ - if (actualTransferCount != NULL) { - *actualTransferCount = myTransferCount; - if (*actualTransferCount > dataLength) - *actualTransferCount = dataLength; - } - /* - * Also, there is a bug in the combination of System 7.0.1 and the 53C96 - * that may cause the real SCSI Status Byte to be in the Message byte. - */ - if (scsiStatusByte == kScsiStatusGood - && scsiMsgByte == kScsiStatusCheckCondition) - scsiStatusByte = kScsiStatusCheckCondition; - if (status == noErr) { - switch (scsiStatusByte) { - case kScsiStatusGood: break; - case kScsiStatusBusy: status = scsiBusy; break; - case 0xFF: status = scsiProvideFail; break; - default: status = scsiNonZeroStatus; break; - } - } - if (status == noErr - && (scsiFlags & scsiDirectionMask) != scsiDirectionNone - && myTransferCount != dataLength) - status = scsiDataRunError; - if (scsiStatusBytePtr != NULL) - *scsiStatusBytePtr = scsiStatusByte; - return (status); -} - -UInt16 -GetCommandLength( - const SCSI_CommandPtr cmdPtr - ) -{ - unsigned short result; - /* - * Look at the "group code" in the command operation. Return zero - * error for the reserved (3, 4) and vendor-specific command (6, 7) - * command groups. Otherwise, set the command length from the group code - * value as specified in the SCSI-II spec. - */ - switch (cmdPtr->scsi6.opcode & 0xE0) { - case (0 << 5): result = 6; break; - case (1 << 5): - case (2 << 5): result = 10; break; - case (5 << 5): result = 12; break; - default: result = 0; break; - } - return (result); -} - -Boolean -IsVirtualMemoryRunning(void) -{ - OSErr status; - long response; - - status = Gestalt(gestaltVMAttr, &response); - /* - * VM is active iff Gestalt succeeded and the response is appropriate. - */ - return (status == noErr && ((response & (1 << gestaltVMPresent)) != 0)); -} diff --git a/DoTestUnitReady.c b/DoTestUnitReady.c deleted file mode 100644 index b9b320b..0000000 --- a/DoTestUnitReady.c +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 1993-97 by Apple Computer, Inc. - * All Rights Reserved - * - * Permission to use, copy, modify, and distribute this software and - * its documentation for any purpose and without fee is hereby granted, - * provided that the above copyright notice appears in all copies and - * that both the copyright notice and this permission notice appear in - * supporting documentation. - * - * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE - * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR - * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, - * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION - * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ -#include -#include "SCSIStuff.h" - -int -DoTestUnitReady( - UInt8 targetID - ) -{ - OSErr status; - Str255 errorText; - char* msg; - static const SCSI_6_Byte_Command gTestUnitReadyCommand = { - kScsiCmdTestUnitReady, 0, 0, 0, 0, 0 - }; - SCSI_Sense_Data senseData; - DeviceIdent scsiDevice; - int rtn_value; - - scsiDevice.diReserved = 0; - scsiDevice.bus = kOriginalSCSIBusAdaptor; - scsiDevice.targetID = targetID; - scsiDevice.LUN = 0; - - status = DoSCSICommand( - scsiDevice, - "\pTest Unit Ready", - (SCSI_CommandPtr) &gTestUnitReadyCommand, - NULL, - 0, - scsiDirectionNone, - NULL, - &senseData, - errorText - ); - if (status == scsiNonZeroStatus) { - msg = "Unknown problem"; - switch (senseData.senseKey & kScsiSenseKeyMask) { - case kScsiSenseIllegalReq: - msg = "Logical Unit Not Supported"; - break; - case kScsiSenseNotReady: - switch ((senseData.additionalSenseCode << 8) - | senseData.additionalSenseQualifier) { - case 0x0500: - msg = "Logical Unit does not respond to selection"; - break; - case 0x0401: - msg = "Logical Unit is becoming ready"; - break; - case 0x0400: - msg = "Logical Unit is not ready. No specific cause."; - break; - case 0x0402: - msg = "Logical Unit is not ready. Unit needs start command."; - break; - case 0x0403: - msg = "Logical Unit is not ready. Unit needs manual intervention."; - break; - case 0x0404: - msg = "Logical Unit is not ready. Format in progress"; - break; - case 0x2500: - msg = "Logical Unit is not supported"; - break; - } - } - rtn_value = -1; - } else if (status != noErr) { - msg = "Test Unit Ready failed"; - rtn_value = 0; - } else { - msg = "Okay - device is ready"; - rtn_value = 1; - } - //printf("%s\n", msg); - return rtn_value; -} - - - diff --git a/MacSCSICommand.h b/MacSCSICommand.h deleted file mode 100644 index b02b66f..0000000 --- a/MacSCSICommand.h +++ /dev/null @@ -1,420 +0,0 @@ -/* - File: MacSCSICommand.h - - Contains: SCSI specific definitions. - - Written by: Martin Minow - -*/ - -/* - * Copyright 1995, 1997 by Apple Computer, Inc. - * All Rights Reserved - * - * Permission to use, copy, modify, and distribute this software and - * its documentation for any purpose and without fee is hereby granted, - * provided that the above copyright notice appears in all copies and - * that both the copyright notice and this permission notice appear in - * supporting documentation. - * - * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE - * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR - * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, - * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION - * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * Scsi-specific definitions. - */ -#ifndef __MacSCSICommand__ -#define __MacSCSICommand__ - -/* - * The 6-byte commands are used for most simple - * I/O requests. - */ -struct SCSI_6_Byte_Command { /* Six-byte command */ - unsigned char opcode; /* 0 */ - unsigned char lbn3; /* 1 lbn in low 5 */ - unsigned char lbn2; /* 2 */ - unsigned char lbn1; /* 3 */ - unsigned char len; /* 4 */ - unsigned char ctrl; /* 5 */ -}; -typedef struct SCSI_6_Byte_Command SCSI_6_Byte_Command; - -struct SCSI_10_Byte_Command { /* Ten-byte command */ - unsigned char opcode; /* 0 */ - unsigned char lun; /* 1 */ - unsigned char lbn4; /* 2 */ - unsigned char lbn3; /* 3 */ - unsigned char lbn2; /* 4 */ - unsigned char lbn1; /* 5 */ - unsigned char pad; /* 6 */ - unsigned char len2; /* 7 */ - unsigned char len1; /* 8 */ - unsigned char ctrl; /* 9 */ -}; -typedef struct SCSI_10_Byte_Command SCSI_10_Byte_Command; - -struct SCSI_12_Byte_Command { /* Twelve-byte command */ - unsigned char opcode; /* 0 */ - unsigned char lun; /* 1 */ - unsigned char lbn4; /* 2 */ - unsigned char lbn3; /* 3 */ - unsigned char lbn2; /* 4 */ - unsigned char lbn1; /* 5 */ - unsigned char len4; /* 6 */ - unsigned char len3; /* 7 */ - unsigned char len2; /* 8 */ - unsigned char len1; /* 9 */ - unsigned char pad; /* 10 */ - unsigned char ctrl; /* 11 */ -}; -typedef struct SCSI_12_Byte_Command SCSI_12_Byte_Command; - -/* - * This union defines all scsi commands. - */ -union SCSI_Command { - SCSI_6_Byte_Command scsi6; - SCSI_10_Byte_Command scsi10; - SCSI_12_Byte_Command scsi12; - unsigned char scsi[12]; -}; -typedef union SCSI_Command SCSI_Command, *SCSI_CommandPtr; - -/* - * Returned by a read-capacity command. - */ -struct SCSI_Capacity_Data { - unsigned char lbn4; /* Number */ - unsigned char lbn3; /* of */ - unsigned char lbn2; /* logical */ - unsigned char lbn1; /* blocks */ - unsigned char len4; /* Length */ - unsigned char len3; /* of each */ - unsigned char len2; /* logical block */ - unsigned char len1; /* in bytes */ -}; -typedef struct SCSI_Capacity_Data SCSI_Capacity_Data; - -struct SCSI_Inquiry_Data { /* Inquiry returns this */ - unsigned char devType; /* 0 Device type, */ - unsigned char devTypeMod; /* 1 Device type modifier */ - unsigned char version; /* 2 ISO/ECMA/ANSI version */ - unsigned char format; /* 3 Response data format */ - unsigned char length; /* 4 Additional Length */ - unsigned char reserved5; /* 5 Reserved */ - unsigned char reserved6; /* 6 Reserved */ - unsigned char flags; /* 7 Capability flags */ - unsigned char vendor[8]; /* 8-15 Vendor-specific */ - unsigned char product[16]; /* 16-31 Product id */ - unsigned char revision[4]; /* 32-35 Product revision */ - unsigned char vendorSpecific[20]; /* 36-55 Vendor stuff */ - unsigned char moreReserved[40]; /* 56-95 Reserved */ -}; -typedef struct SCSI_Inquiry_Data SCSI_Inquiry_Data; - -/* - * This bit may be set in SCSI_Inquiry_Data.devTypeMod - */ -enum { - kScsiInquiryRMB = 0x80 /* Removable medium if set */ -}; -/* - * These bits may be set in SCSI_Inquiry_Data.flags - */ -enum { - kScsiInquiryRelAdr = 0x80, /* Has relative addressing */ - kScsiInquiryWBus32 = 0x40, /* Wide (32-bit) transfers */ - kScsiInquiryWBus16 = 0x20, /* Wide (16-bit) transfers */ - kScsiInquirySync = 0x10, /* Synchronous transfers */ - kScsiInquiryLinked = 0x08, /* Linked commands ok */ - kScsiInquiryReserved = 0x04, - kScsiInquiryCmdQue = 0x02, /* Tagged cmd queuing ok */ - kScsiInquirySftRe = 0x01 /* Soft reset alternative */ -}; - -/* - * These bits may be set in SCSI_Inquiry_Data.devType - */ -enum { - kScsiDevTypeDirect = 0, - kScsiDevTypeSequential, - kScsiDevTypePrinter, - kScsiDevTypeProcessor, - kScsiDevTypeWorm, /* Write-once, read mult */ - kScsiDevTypeCDROM, - kScsiDevTypeScanner, - kScsiDevTypeOptical, - kScsiDevTypeChanger, - kScsiDevTypeComm, - kScsiDevTypeGraphicArts0A, - kScsiDevTypeGraphicArts0B, - kScsiDevTypeFirstReserved, /* Reserved sequence start */ - kScsiDevTypeUnknownOrMissing = 0x1F, - kScsiDevTypeMask = 0x1F -}; -/* - * These are device type qualifiers. We need them to distinguish between "unknown" - * and "missing" devices. - */ -enum { - kScsiDevTypeQualifierConnected = 0x00, /* Exists and is connected */ - kScsiDevTypeQualifierNotConnected = 0x20, /* Logical unit exists */ - kScsiDevTypeQualifierReserved = 0x40, - kScsiDevTypeQualifierMissing = 0x60, /* No such logical unit */ - kScsiDevTypeQualifierVendorSpecific = 0x80, /* Other bits are unspecified */ - kScsiDevTypeQualifierMask = 0xE0 -}; -#define kScsiDevTypeMissing \ - (kScsiDevTypeUnknownOrMissing | kScsiDevTypeQualifierMissing) - -/* - * This is the data that is returned after a GetExtendedStatus - * request. The errorCode gives a general indication of the error, - * which may be qualified by the additionalSenseCode and - * additionalSenseQualifier fields. These may be device (vendor) - * specific values, however. The info[] field contains additional - * information. For a media error, it contains the failing - * logical block number (most-significant byte first). - */ -struct SCSI_Sense_Data { /* Request Sense result */ - unsigned char errorCode; /* 0 Class code, valid lbn */ - unsigned char segmentNumber; /* 1 Segment number */ - unsigned char senseKey; /* 2 Sense key and flags */ - unsigned char info[4]; - unsigned char additionalSenseLength; - unsigned char reservedForCopy[4]; - unsigned char additionalSenseCode; - unsigned char additionalSenseQualifier; - unsigned char fruCode; /* Field replacable unit code */ - unsigned char senseKeySpecific[2]; - unsigned char additional[101]; -}; -typedef struct SCSI_Sense_Data SCSI_Sense_Data; -/* - * The high-bit of errorCode signals whether there is a logical - * block. The low value signals whether there is a valid sense - */ -#define kScsiSenseHasLBN 0x80 /* Logical block number set */ -#define kScsiSenseInfoValid 0x70 /* Is sense key valid? */ -#define kScsiSenseInfoMask 0x70 /* Mask for sense info */ -/* - * These bits may be set in the sense key - */ -#define kScsiSenseKeyMask 0x0F -#define kScsiSenseILI 0x20 /* Illegal logical Length */ -#define kScsiSenseEOM 0x40 /* End of media */ -#define kScsiSenseFileMark 0x80 /* End of file mark */ - -/* - * SCSI sense codes. (Returned after request sense). - */ -#define kScsiSenseNone 0x00 /* No error */ -#define kScsiSenseRecoveredErr 0x01 /* Warning */ -#define kScsiSenseNotReady 0x02 /* Device not ready */ -#define kScsiSenseMediumErr 0x03 /* Device medium error */ -#define kScsiSenseHardwareErr 0x04 /* Device hardware error */ -#define kScsiSenseIllegalReq 0x05 /* Illegal request for dev. */ -#define kScsiSenseUnitAtn 0x06 /* Unit attention (not err) */ -#define kScsiSenseDataProtect 0x07 /* Data protection */ -#define kScsiSenseBlankCheck 0x08 /* Tape-specific error */ -#define kScsiSenseVendorSpecific 0x09 /* Vendor-specific error */ -#define kScsiSenseCopyAborted 0x0a /* Copy request cancelled */ -#define kScsiSenseAbortedCmd 0x0b /* Initiator aborted cmd. */ -#define kScsiSenseEqual 0x0c /* Comparison equal */ -#define kScsiSenseVolumeOverflow 0x0d /* Write past end mark */ -#define kScsiSenseMiscompare 0x0e /* Comparison failed */ -#define kScsiSenseCurrentErr 0x70 -#define kScsiSenseDeferredErr 0x71 - -/* - * Mode sense parameter header - */ -struct SCSI_ModeParamHeader { - unsigned char modeDataLength; - unsigned char mediumType; - unsigned char deviceSpecific; - unsigned char blockDescriptorLength; -}; -typedef struct SCSI_ModeParamHeader SCSI_ModeParamHeader; - -struct SCSI_ModeParamBlockDescriptor { - unsigned char densityCode; - unsigned char numberOfBlocks[3]; - unsigned char reserved; - unsigned char blockLength[3]; -}; -typedef struct SCSI_ModeParamBlockDescriptor SCSI_ModeParamBlockDescriptor; - -union SCSI_ModeParamPage { - unsigned char data[1]; - struct { - unsigned char code; - unsigned char length; - } page; -}; -typedef union SCSI_ModeParamPage SCSI_ModeParamPage; - -/* - * LogSense parameter header - */ -struct SCSI_LogSenseParamHeader { - unsigned char pageCode; - unsigned char reserved; - unsigned char pageLength[2]; -}; -typedef struct SCSI_LogSenseParamHeader SCSI_LogSenseParamHeader; - -/* - * Log parameter pages are variable-length with a fixed length header. - */ -union SCSI_LogSenseParamPage { - unsigned char data[1]; - struct { - unsigned char parameterCode[2]; - unsigned char flags; - unsigned char parameterLength; - } page; -}; -typedef union SCSI_LogSenseParamPage SCSI_LogSenseParamPage; - -/* - * SCSI command status (from status phase) - */ -#define kScsiStatusGood 0x00 /* Normal completion */ -#define kScsiStatusCheckCondition 0x02 /* Need GetExtendedStatus */ -#define kScsiStatusConditionMet 0x04 -#define kScsiStatusBusy 0x08 /* Device busy (self-test?) */ -#define kScsiStatusIntermediate 0x10 /* Intermediate status */ -#define kScsiStatusResConflict 0x18 /* Reservation conflict */ -#define kScsiStatusQueueFull 0x28 /* Target can't do command */ -#define kScsiStatusReservedMask 0x3e /* Vendor specific? */ - -/* - * SCSI command codes. Commands defined as ...6, ...10, ...12, are - * six-byte, ten-byte, and twelve-byte variants of the indicated command. - */ -/* - * These commands are supported for all devices. - */ -#define kScsiCmdChangeDefinition 0x40 -#define kScsiCmdCompare 0x39 -#define kScsiCmdCopy 0x18 -#define kScsiCmdCopyAndVerify 0x3a -#define kScsiCmdInquiry 0x12 -#define kScsiCmdLogSelect 0x4c -#define kScsiCmdLogSense 0x4d -#define kScsiCmdModeSelect10 0x55 -#define kScsiCmdModeSelect6 0x15 -#define kScsiCmdModeSense10 0x5a -#define kScsiCmdModeSense6 0x1a -#define kScsiCmdReadBuffer 0x3c -#define kScsiCmdRecvDiagResult 0x1c -#define kScsiCmdRequestSense 0x03 -#define kScsiCmdSendDiagnostic 0x1d -#define kScsiCmdTestUnitReady 0x00 -#define kScsiCmdWriteBuffer 0x3b - -/* - * These commands are supported by direct-access devices only. - */ -#define kScsiCmdFormatUnit 0x04 -#define kSCSICmdCopy 0x18 -#define kSCSICmdCopyAndVerify 0x3a -#define kScsiCmdLockUnlockCache 0x36 -#define kScsiCmdPrefetch 0x34 -#define kScsiCmdPreventAllowRemoval 0x1e -#define kScsiCmdRead6 0x08 -#define kScsiCmdRead10 0x28 -#define kScsiCmdReadCapacity 0x25 -#define kScsiCmdReadDefectData 0x37 -#define kScsiCmdReadLong 0x3e -#define kScsiCmdReassignBlocks 0x07 -#define kScsiCmdRelease 0x17 -#define kScsiCmdReserve 0x16 -#define kScsiCmdRezeroUnit 0x01 -#define kScsiCmdSearchDataEql 0x31 -#define kScsiCmdSearchDataHigh 0x30 -#define kScsiCmdSearchDataLow 0x32 -#define kScsiCmdSeek6 0x0b -#define kScsiCmdSeek10 0x2b -#define kScsiCmdSetLimits 0x33 -#define kScsiCmdStartStopUnit 0x1b -#define kScsiCmdSynchronizeCache 0x35 -#define kScsiCmdVerify 0x2f -#define kScsiCmdWrite6 0x0a -#define kScsiCmdWrite10 0x2a -#define kScsiCmdWriteAndVerify 0x2e -#define kScsiCmdWriteLong 0x3f -#define kScsiCmdWriteSame 0x41 - -/* - * These commands are supported by sequential devices. - */ -#define kScsiCmdRewind 0x01 -#define kScsiCmdWriteFilemarks 0x10 -#define kScsiCmdSpace 0x11 -#define kScsiCmdLoadUnload 0x1B -/* - * ANSI SCSI-II for CD-ROM devices. - */ -#define kScsiCmdReadCDTableOfContents 0x43 - -/* - * Message codes (for Msg In and Msg Out phases). - */ -#define kScsiMsgAbort 0x06 -#define kScsiMsgAbortTag 0x0d -#define kScsiMsgBusDeviceReset 0x0c -#define kScsiMsgClearQueue 0x0e -#define kScsiMsgCmdComplete 0x00 -#define kScsiMsgDisconnect 0x04 -#define kScsiMsgIdentify 0x80 -#define kScsiMsgIgnoreWideResdue 0x23 -#define kScsiMsgInitiateRecovery 0x0f -#define kScsiMsgInitiatorDetectedErr 0x05 -#define kScsiMsgLinkedCmdComplete 0x0a -#define kScsiMsgLinkedCmdCompleteFlag 0x0b -#define kScsiMsgParityErr 0x09 -#define kScsiMsgRejectMsg 0x07 -#define kScsiMsgModifyDataPtr 0x00 /* Extended msg */ -#define kScsiMsgNop 0x08 -#define kScsiMsgHeadOfQueueTag 0x21 /* Two byte msg */ -#define kScsiMsgOrderedQueueTag 0x22 /* Two byte msg */ -#define kScsiMsgSimpleQueueTag 0x20 /* Two byte msg */ -#define kScsiMsgReleaseRecovery 0x10 -#define kScsiMsgRestorePointers 0x03 -#define kScsiMsgSaveDataPointers 0x02 -#define kScsiMsgSyncXferReq 0x01 /* Extended msg */ -#define kScsiMsgWideDataXferReq 0x03 /* Extended msg */ -#define kScsiMsgTerminateIOP 0x11 -#define kScsiMsgExtended 0x01 -#define kScsiMsgEnableDisconnectMask 0x40 - -#define kScsiMsgTwoByte 0x20 -#define kScsiMsgTwoByteMin 0x20 -#define kScsiMsgTwoByteMax 0x2f - -/* - * Default timeout times for SCSI commands (times are in Msec). - */ -#define kScsiNormalCompletionTime (500L) /* 1/2 second */ -/* - * Dratted DAT tape. - */ -#define kScsiDATCompletionTime (60L * 1000L); /* One minute */ -/* - * Yes, we do allow 90 seconds for spin-up of those dratted tape drives. - */ -#define kScsiSpinUpCompletionTime (90L * 1000L) - - -#endif /* __MacSCSICommand__ */ diff --git a/Makefile b/Makefile index 0adf886..c60dd5b 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,10 @@ -all: pdisk fdisk +CFLAGS=-std=gnu99 +all: pdisk pdisk: pdisk.o dump.o partition_map.o convert.o io.o errors.o bitfield.o -fdisk: fdisk.o fdisklabel.o - clean: - rm -f *.o pdisk fdisk mac-fdisk pmac-fdisk - -install: pdisk fdisk - -rm -f pmac-fdisk mac-fdisk - ln pdisk mac-fdisk - ln fdisk pmac-fdisk - install -o root -g root -m 0755 mac-fdisk ${DESTDIR}/sbin - install -o root -g root -m 0755 pmac-fdisk ${DESTDIR}/sbin - -distribution: - cd ..; tar cvf pdisk.src.tar.`date +%y%m%d` --files-from pdisk/list.src - tar cvf ../pdisk.bin.tar.`date +%y%m%d` pdisk fdisk pdisk.8 - cp pdisk.hqx ../pdisk.hqx.`date +%y%m%d` + rm -f *.o pdisk convert.o: convert.c partition_map.h convert.h dump.o: dump.c io.h errors.h partition_map.h diff --git a/SCSIStuff.h b/SCSIStuff.h deleted file mode 100644 index 2de563e..0000000 --- a/SCSIStuff.h +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright 1993-97 by Apple Computer, Inc. - * All Rights Reserved - * - * Permission to use, copy, modify, and distribute this software and - * its documentation for any purpose and without fee is hereby granted, - * provided that the above copyright notice appears in all copies and - * that both the copyright notice and this permission notice appear in - * supporting documentation. - * - * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE - * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR - * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, - * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION - * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef __SCSIExplorer__ -#define __SCSIExplorer__ -#define DEBUG_INFOLIST 0 - -#if !defined(__NewTypesDefined__) -#define __NewTypesDefined__ -typedef signed char SInt8; -typedef signed short SInt16; -typedef signed long SInt32; -typedef unsigned char UInt8; -typedef unsigned short UInt16; -typedef unsigned long UInt32; -typedef unsigned long ItemCount; -typedef unsigned long ByteCount; -#endif - -/* - * Note: this must be SCSI.h from Universal Headers 2.0 - the current version - * of Think C headers still has the "old" header without SCSI Manager 4.3 support. - */ -#include - -#include "MacSCSICommand.h" - -#ifndef LOG -#define LOG(what) /* Nothing */ -#endif /* LOG */ - -/* - *** Common definitions - */ -#ifndef EXTERN -#define EXTERN extern -#endif -#ifndef TRUE -#define TRUE 1 -#define FALSE 0 -#endif -#ifndef NULL -#define NULL 0 -#endif - -enum { - bit0 = (1 << 0), - bit1 = (1 << 1), - bit2 = (1 << 2), - bit3 = (1 << 3), - bit4 = (1 << 4), - bit5 = (1 << 5), - bit6 = (1 << 6), - bit7 = (1 << 7) -}; - -#define kOriginalSCSIBusAdaptor (0xFF) -#define kEndOfLine 0x0D /* Return */ - - -#define UNUSED(what) do { \ - what = what; \ - } while (0) - - -#define FourBytes(hiByteAddr) ( \ - ( (((UInt32) (((UInt8 *) &(hiByteAddr)))[0]) << 24) \ - | (((UInt32) (((UInt8 *) &(hiByteAddr)))[1]) << 16) \ - | (((UInt32) (((UInt8 *) &(hiByteAddr)))[2]) << 8) \ - | (((UInt32) (((UInt8 *) &(hiByteAddr)))[3]) << 0) \ - )) -#define ThreeBytes(hiByteAddr) ( \ - ( (((UInt32) (((UInt8 *) &(hiByteAddr)))[0]) << 16) \ - | (((UInt32) (((UInt8 *) &(hiByteAddr)))[1]) << 8) \ - | (((UInt32) (((UInt8 *) &(hiByteAddr)))[2]) << 0) \ - )) -#define TwoBytes(hiByteAddr) ( \ - ( (((UInt32) (((UInt8 *) &(hiByteAddr)))[0]) << 8) \ - | (((UInt32) (((UInt8 *) &(hiByteAddr)))[1]) << 0) \ - )) - -#define SameSCSIDevice(a, b) ((*((UInt32 *) &a)) == (*((UInt32 *) &b))) -int DoTestUnitReady(UInt8 targetID); - -Boolean IsIllegalRequest( - OSErr scsiStatus, - const SCSI_Sense_Data *senseDataPtr - ); -Boolean IsNoMedia( - OSErr scsiStatus, - const SCSI_Sense_Data *senseDataPtr - ); - -/* - * All SCSI Commands come here. - * if scsiDevice.busID == kOriginalSCSIBusAdaptor, IM-IV SCSI will be called. - * scsiFlags should be scsiDirectionNone, scsiDirectionIn, or scsiDirectionOut - * actualTransferCount may be NULL if you don't care. - * Both old and new SCSI return SCSI Manager 4.3 errors. - * - * DoSCSICommand throws really serious errors, but returns SCSI errors such - * as dataRunError and scsiDeviceNotThere. - */ -OSErr DoSCSICommand( - DeviceIdent scsiDevice, - ConstStr255Param currentAction, - const SCSI_CommandPtr callerSCSICommand, - Ptr dataBuffer, - ByteCount dataLength, - UInt32 scsiFlags, - ByteCount *actualTransferCount, - SCSI_Sense_Data *sensePtr, - StringPtr senseMessage - ); - -/* - * Cheap 'n dirty memory clear routine. - */ -#define CLEAR(dst) ClearMemory((void *) &dst, sizeof dst) -void ClearMemory( - void *dataArea, - ByteCount dataSize - ); - -/* - * Global values - */ -EXTERN SCSIExecIOPB *gSCSIExecIOPBPtr; -EXTERN UInt32 gSCSIExecIOPBPtrLen; - -#endif /* __SCSIExplorer__ */ diff --git a/SCSI_misc.c b/SCSI_misc.c deleted file mode 100644 index 08c25fb..0000000 --- a/SCSI_misc.c +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 1997 by Apple Computer, Inc. - * All Rights Reserved - * - * Permission to use, copy, modify, and distribute this software and - * its documentation for any purpose and without fee is hereby granted, - * provided that the above copyright notice appears in all copies and - * that both the copyright notice and this permission notice appear in - * supporting documentation. - * - * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE - * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE. - * - * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR - * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, - * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION - * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include "SCSIStuff.h" - SCSIExecIOPB *gSCSIExecIOPBPtr; - UInt32 gSCSIExecIOPBPtrLen; - -void -ClearMemory( - void *dataPtr, - register unsigned long size - ) -{ - register char *ptr; - - ptr = (char *) dataPtr; - while (size > 0) { - *ptr++ = 0; - --size; - } -} diff --git a/bitfield.c b/bitfield.c index 687e8bd..3bc840c 100644 --- a/bitfield.c +++ b/bitfield.c @@ -29,20 +29,10 @@ #include "bitfield.h" -// -// Defines -// - - -// -// Types -// - - // // Global Constants // -const unsigned long masks[] = { +static const uint32_t masks[] = { 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000F, 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF, @@ -54,48 +44,27 @@ const unsigned long masks[] = { 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF }; -// -// Global Variables -// - - -// -// Forward declarations -// - - -// -// Routines -// -unsigned long -bitfield_set(unsigned long *bf, int base, int length, unsigned long value) +uint32_t +bitfield_set(uint32_t *bf, int base, int length, uint32_t value) { - unsigned long t; - unsigned long m; - int s; - int i; - // compute shift & mask, coerce value to correct number of bits, // zap the old bits and stuff the new value // return the masked value in case someone wants it. - s = (base + 1) - length; - m = masks[length]; - t = value & m; + int s = (base + 1) - length; + uint32_t m = masks[length]; + uint32_t t = value & m; *bf = (*bf & ~(m << s)) | (t << s); return t; } -unsigned long -bitfield_get(unsigned long bf, int base, int length) +uint32_t +bitfield_get(uint32_t bf, int base, int length) { - unsigned long m; - int s; - int i; - // compute shift & mask // return the correct number of bits (shifted to low end) - s = (base + 1) - length; - m = masks[length]; + int s = (base + 1) - length; + uint32_t m = masks[length]; return ((bf >> s) & m); } + diff --git a/bitfield.h b/bitfield.h index ff56759..cc0b973 100644 --- a/bitfield.h +++ b/bitfield.h @@ -39,29 +39,12 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#ifndef bitfield_h +#define bitfield_h -// -// Defines -// - - -// -// Types -// - - -// -// Global Constants -// - - -// -// Global Variables -// +#include +uint32_t bitfield_set(uint32_t *bf, int base, int length, uint32_t value); +uint32_t bitfield_get(uint32_t bf, int base, int length); -// -// Forward declarations -// -unsigned long bitfield_set(unsigned long *bf, int base, int length, unsigned long value); -unsigned long bitfield_get(unsigned long bf, int base, int length); +#endif diff --git a/convert.c b/convert.c index 977ff9a..7f01911 100644 --- a/convert.c +++ b/convert.c @@ -27,180 +27,125 @@ * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include - -#ifdef __linux__ -#include -#else -#define LITTLE_ENDIAN 1234 -#define BIG_ENDIAN 4321 -#define BYTE_ORDER 4321 -#endif - #include "partition_map.h" #include "convert.h" +#include -// -// Defines -// - - -// -// Types -// - - -// -// Global Constants -// - - -// -// Global Variables -// - - -// -// Forward declarations -// -void reverse2(u8 *bytes); -void reverse4(u8 *bytes); - - -// -// Routines -// int convert_dpme(DPME *data, int to_cpu_form) { -#if BYTE_ORDER == LITTLE_ENDIAN - // Since we will toss the block if the signature doesn't match - // we don't need to check the signature down here. - reverse2((u8 *)&data->dpme_signature); - reverse2((u8 *)&data->dpme_reserved_1); - reverse4((u8 *)&data->dpme_map_entries); - reverse4((u8 *)&data->dpme_pblock_start); - reverse4((u8 *)&data->dpme_pblocks); - reverse4((u8 *)&data->dpme_lblock_start); - reverse4((u8 *)&data->dpme_lblocks); - reverse4((u8 *)&data->dpme_flags); - reverse4((u8 *)&data->dpme_boot_block); - reverse4((u8 *)&data->dpme_boot_bytes); - reverse4((u8 *)&data->dpme_load_addr); - reverse4((u8 *)&data->dpme_load_addr_2); - reverse4((u8 *)&data->dpme_goto_addr); - reverse4((u8 *)&data->dpme_goto_addr_2); - reverse4((u8 *)&data->dpme_checksum); - convert_bzb((BZB *)data->dpme_bzb, to_cpu_form); -#endif - return 0; + // Since we will toss the block if the signature doesn't match + // we don't need to check the signature down here. + data->dpme_signature = htons(data->dpme_signature); + data->dpme_reserved_1 = htons(data->dpme_reserved_1); + data->dpme_map_entries = htonl(data->dpme_map_entries); + data->dpme_pblock_start = htonl(data->dpme_pblock_start); + data->dpme_pblocks = htonl(data->dpme_pblocks); + data->dpme_lblock_start = htonl(data->dpme_lblock_start); + data->dpme_lblocks = htonl(data->dpme_lblocks); + data->dpme_flags = htonl(data->dpme_flags); + data->dpme_boot_block = htonl(data->dpme_boot_block); + data->dpme_boot_bytes = htonl(data->dpme_boot_bytes); + data->dpme_load_addr = htonl(data->dpme_load_addr); + data->dpme_load_addr_2 = htonl(data->dpme_load_addr_2); + data->dpme_goto_addr = htonl(data->dpme_goto_addr); + data->dpme_goto_addr_2 = htonl(data->dpme_goto_addr_2); + data->dpme_checksum = htonl(data->dpme_checksum); + convert_bzb((BZB *)data->dpme_bzb, to_cpu_form); + return 0; } - -#if BYTE_ORDER == LITTLE_ENDIAN int convert_bzb(BZB *data, int to_cpu_form) { - // Since the data here varies according to the type of partition we - // do not want to convert willy-nilly. We use the flag to determine - // whether to check for the signature before or after we flip the bytes. - if (to_cpu_form) { - reverse4((u8 *)&data->bzb_magic); - if (data->bzb_magic != BZBMAGIC) { - reverse4((u8 *)&data->bzb_magic); - if (data->bzb_magic != BZBMAGIC) { - return 0; - } + // Since the data here varies according to the type of partition we + // do not want to convert willy-nilly. We use the flag to determine + // whether to check for the signature before or after we flip the bytes. + if (to_cpu_form) + { + data->bzb_magic = htonl(data->bzb_magic); + if (data->bzb_magic != BZBMAGIC) + { + data->bzb_magic = htonl(data->bzb_magic); + if (data->bzb_magic != BZBMAGIC) + { + return 0; + } + } } - } else { - if (data->bzb_magic != BZBMAGIC) { - return 0; + else + { + if (data->bzb_magic != BZBMAGIC) + { + return 0; + } + data->bzb_magic = htonl(data->bzb_magic); } - reverse4((u8 *)&data->bzb_magic); - } - reverse2((u8 *)&data->bzb_inode); - reverse4((u8 *)&data->bzb_flags); - reverse4((u8 *)&data->bzb_tmade); - reverse4((u8 *)&data->bzb_tmount); - reverse4((u8 *)&data->bzb_tumount); - return 0; + data->bzb_inode = htons(data->bzb_inode); + data->bzb_flags = htonl(data->bzb_flags); + data->bzb_tmade = htonl(data->bzb_tmade); + data->bzb_tmount = htonl(data->bzb_tmount); + data->bzb_tumount = htonl(data->bzb_tumount); + return 0; } -#endif int convert_block0(Block0 *data, int to_cpu_form) { -#if BYTE_ORDER == LITTLE_ENDIAN - DDMap *m; - u16 count; - int i; - - // Since this data is optional we do not want to convert willy-nilly. - // We use the flag to determine whether to check for the signature - // before or after we flip the bytes and to determine which form of - // the count to use. - if (to_cpu_form) { - reverse2((u8 *)&data->sbSig); - if (data->sbSig != BLOCK0_SIGNATURE) { - reverse2((u8 *)&data->sbSig); - if (data->sbSig != BLOCK0_SIGNATURE) { - return 0; - } + // Since this data is optional we do not want to convert willy-nilly. + // We use the flag to determine whether to check for the signature + // before or after we flip the bytes and to determine which form of + // the count to use. + if (to_cpu_form) + { + data->sbSig = htons(data->sbSig); + if (data->sbSig != BLOCK0_SIGNATURE) + { + data->sbSig = htons(data->sbSig); + if (data->sbSig != BLOCK0_SIGNATURE) + { + return 0; + } + } } - } else { - if (data->sbSig != BLOCK0_SIGNATURE) { - return 0; + else + { + if (data->sbSig != BLOCK0_SIGNATURE) + { + return 0; + } + data->sbSig = htons(data->sbSig); } - reverse2((u8 *)&data->sbSig); - } - reverse2((u8 *)&data->sbBlkSize); - reverse4((u8 *)&data->sbBlkCount); - reverse2((u8 *)&data->sbDevType); - reverse2((u8 *)&data->sbDevId); - reverse4((u8 *)&data->sbData); - if (to_cpu_form) { - reverse2((u8 *)&data->sbDrvrCount); - count = data->sbDrvrCount; - } else { - count = data->sbDrvrCount; - reverse2((u8 *)&data->sbDrvrCount); - } - - if (count > 0) { - m = (DDMap *) data->sbMap; - for (i = 0; i < count; i++) { - reverse4((u8 *)&m[i].ddBlock); - reverse2((u8 *)&m[i].ddSize); - reverse2((u8 *)&m[i].ddType); + data->sbBlkSize = htons(data->sbBlkSize); + data->sbBlkCount = htonl(data->sbBlkCount); + data->sbDevType = htons(data->sbDevType); + data->sbDevId = htons(data->sbDevId); + data->sbData = htonl(data->sbData); + + uint16_t count; + if (to_cpu_form) + { + data->sbDrvrCount = htons(data->sbDrvrCount); + count = data->sbDrvrCount; + } + else + { + count = data->sbDrvrCount; + data->sbDrvrCount = htons(data->sbDrvrCount); } - } -#endif - return 0; -} - - -void -reverse2(u8 *bytes) -{ - u8 t; - t = *bytes; - *bytes = bytes[1]; - bytes[1] = t; + if (count > 0) + { + DDMap* m = (DDMap *) data->sbMap; + for (int i = 0; i < count; i++) + { + m[i].ddBlock = htonl(m[i].ddBlock); + m[i].ddSize = htons(m[i].ddSize); + m[i].ddType = htons(m[i].ddType); + } + } + return 0; } - -void -reverse4(u8 *bytes) -{ - u8 t; - - t = *bytes; - *bytes = bytes[3]; - bytes[3] = t; - t = bytes[1]; - bytes[1] = bytes[2]; - bytes[2] = t; -} diff --git a/convert.h b/convert.h index 269e192..1c323e3 100644 --- a/convert.h +++ b/convert.h @@ -28,31 +28,14 @@ * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#ifndef convert_h +#define convert_h +#include "dpme.h" -// -// Defines -// - - -// -// Types -// - - -// -// Global Constants -// - - -// -// Global Variables -// - - -// -// Forward declarations -// int convert_block0(Block0 *data, int to_cpu_form); int convert_bzb(BZB *data, int to_cpu_form); int convert_dpme(DPME *data, int to_cpu_form); + +#endif + diff --git a/dpme.h b/dpme.h index c2a9958..8ff858c 100644 --- a/dpme.h +++ b/dpme.h @@ -37,8 +37,11 @@ * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#ifndef dpme_h +#define dpme_h #include "bitfield.h" +#include // // Defines @@ -51,83 +54,68 @@ // A/UX only stuff (tradition!) #define dpme_bzb dpme_boot_args #define BZBMAGIC 0xABADBABE /* BZB magic number */ -#define FST ((u8) 0x1) /* standard UNIX FS */ -#define FSTEFS ((u8) 0x2) /* Autorecovery FS */ -#define FSTSFS ((u8) 0x3) /* Swap FS */ - - -// -// Types -// -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned int u32; +#define FST ((uint8_t) 0x1) /* standard UNIX FS */ +#define FSTEFS ((uint8_t) 0x2) /* Autorecovery FS */ +#define FSTSFS ((uint8_t) 0x3) /* Swap FS */ // Physical block zero of the disk has this format struct Block0 { - u16 sbSig; /* unique value for SCSI block 0 */ - u16 sbBlkSize; /* block size of device */ - u32 sbBlkCount; /* number of blocks on device */ - u16 sbDevType; /* device type */ - u16 sbDevId; /* device id */ - u32 sbData; /* not used */ - u16 sbDrvrCount; /* driver descriptor count */ - u16 sbMap[247]; /* descriptor map */ + uint16_t sbSig; /* unique value for SCSI block 0 */ + uint16_t sbBlkSize; /* block size of device */ + uint32_t sbBlkCount; /* number of blocks on device */ + uint16_t sbDevType; /* device type */ + uint16_t sbDevId; /* device id */ + uint32_t sbData; /* not used */ + uint16_t sbDrvrCount; /* driver descriptor count */ + uint16_t sbMap[247]; /* descriptor map */ }; typedef struct Block0 Block0; // Where &sbMap[0] is actually an array DDMap[sbDrvrCount] // kludge to get around alignment junk struct DDMap { - u32 ddBlock; /* 1st driver's starting block */ - u16 ddSize; /* size of 1st driver (512-byte blks) */ - u16 ddType; /* system type (1 for Mac+) */ + uint32_t ddBlock; /* 1st driver's starting block */ + uint16_t ddSize; /* size of 1st driver (512-byte blks) */ + uint16_t ddType; /* system type (1 for Mac+) */ }; typedef struct DDMap DDMap; // Each partition map entry (blocks 1 through n) has this format struct dpme { - u16 dpme_signature ; - u16 dpme_reserved_1 ; - u32 dpme_map_entries ; - u32 dpme_pblock_start ; - u32 dpme_pblocks ; + uint16_t dpme_signature ; + uint16_t dpme_reserved_1 ; + uint32_t dpme_map_entries ; + uint32_t dpme_pblock_start ; + uint32_t dpme_pblocks ; char dpme_name[DPISTRLEN] ; /* name of partition */ char dpme_type[DPISTRLEN] ; /* type of partition */ - u32 dpme_lblock_start ; - u32 dpme_lblocks ; - u32 dpme_flags; + uint32_t dpme_lblock_start ; + uint32_t dpme_lblocks ; + uint32_t dpme_flags; #if 0 - u32 dpme_reserved_2 : 23 ; /* Bit 9 through 31. */ - u32 dpme_os_specific_1 : 1 ; /* Bit 8. */ - u32 dpme_os_specific_2 : 1 ; /* Bit 7. */ - u32 dpme_os_pic_code : 1 ; /* Bit 6. */ - u32 dpme_writable : 1 ; /* Bit 5. */ - u32 dpme_readable : 1 ; /* Bit 4. */ - u32 dpme_bootable : 1 ; /* Bit 3. */ - u32 dpme_in_use : 1 ; /* Bit 2. */ - u32 dpme_allocated : 1 ; /* Bit 1. */ - u32 dpme_valid : 1 ; /* Bit 0. */ -#endif - u32 dpme_boot_block ; - u32 dpme_boot_bytes ; -#ifdef __linux__ - u32 dpme_load_addr ; - u32 dpme_load_addr_2 ; - u32 dpme_goto_addr ; - u32 dpme_goto_addr_2 ; -#else - u8 *dpme_load_addr ; - u8 *dpme_load_addr_2 ; - u8 *dpme_goto_addr ; - u8 *dpme_goto_addr_2 ; + uint32_t dpme_reserved_2 : 23 ; /* Bit 9 through 31. */ + uint32_t dpme_os_specific_1 : 1 ; /* Bit 8. */ + uint32_t dpme_os_specific_2 : 1 ; /* Bit 7. */ + uint32_t dpme_os_pic_code : 1 ; /* Bit 6. */ + uint32_t dpme_writable : 1 ; /* Bit 5. */ + uint32_t dpme_readable : 1 ; /* Bit 4. */ + uint32_t dpme_bootable : 1 ; /* Bit 3. */ + uint32_t dpme_in_use : 1 ; /* Bit 2. */ + uint32_t dpme_allocated : 1 ; /* Bit 1. */ + uint32_t dpme_valid : 1 ; /* Bit 0. */ #endif - u32 dpme_checksum ; + uint32_t dpme_boot_block ; + uint32_t dpme_boot_bytes ; + uint32_t dpme_load_addr ; + uint32_t dpme_load_addr_2 ; + uint32_t dpme_goto_addr ; + uint32_t dpme_goto_addr_2 ; + uint32_t dpme_checksum ; char dpme_process_id[16] ; - u32 dpme_boot_args[32] ; - u32 dpme_reserved_3[62] ; + uint32_t dpme_boot_args[32] ; + uint32_t dpme_reserved_3[62] ; }; typedef struct dpme DPME; @@ -159,9 +147,9 @@ typedef struct dpme DPME; // Alternate block map (aka bad block remaping) [Never really used] struct abm /* altblk map info stored in bzb */ { - u32 abm_size; /* size of map in bytes */ - u32 abm_ents; /* number of used entries */ - u32 abm_start; /* start of altblk map */ + uint32_t abm_size; /* size of map in bytes */ + uint32_t abm_ents; /* number of used entries */ + uint32_t abm_start; /* start of altblk map */ }; typedef struct abm ABM; @@ -170,25 +158,25 @@ typedef struct abm ABM; // kludge to get around alignment junk struct bzb /* block zero block format */ { - u32 bzb_magic; /* magic number */ - u8 bzb_cluster; /* Autorecovery cluster grouping */ - u8 bzb_type; /* FS type */ - u16 bzb_inode; /* bad block inode number */ - u32 bzb_flags; + uint32_t bzb_magic; /* magic number */ + uint8_t bzb_cluster; /* Autorecovery cluster grouping */ + uint8_t bzb_type; /* FS type */ + uint16_t bzb_inode; /* bad block inode number */ + uint32_t bzb_flags; #if 0 - u16 bzb_root:1, /* FS is a root FS */ + uint16_t bzb_root:1, /* FS is a root FS */ bzb_usr:1, /* FS is a usr FS */ bzb_crit:1, /* FS is a critical FS */ bzb_rsrvd:8, /* reserved for later use */ bzb_slice:5; /* slice number to associate with plus one */ - u16 bzb_filler; /* pad bitfield to 32 bits */ + uint16_t bzb_filler; /* pad bitfield to 32 bits */ #endif - u32 bzb_tmade; /* time of FS creation */ - u32 bzb_tmount; /* time of last mount */ - u32 bzb_tumount; /* time of last umount */ + uint32_t bzb_tmade; /* time of FS creation */ + uint32_t bzb_tmount; /* time of last mount */ + uint32_t bzb_tumount; /* time of last umount */ ABM bzb_abm; /* altblk map info */ - u32 bzb_fill2[7]; /* for expansion of ABM (ha!ha!) */ - u8 bzb_mount_point[64]; /* default mount point name */ + uint32_t bzb_fill2[7]; /* for expansion of ABM (ha!ha!) */ + uint8_t bzb_mount_point[64]; /* default mount point name */ }; typedef struct bzb BZB; @@ -202,17 +190,4 @@ typedef struct bzb BZB; #define bzb_crit_get(p) bitfield_get(p->bzb_flags, 29, 1) #define bzb_slice_get(p) bitfield_get(p->bzb_flags, 20, 5) - -// -// Global Constants -// - - -// -// Global Variables -// - - -// -// Forward declarations -// +#endif diff --git a/dump.c b/dump.c index 4153dc5..77c2b49 100644 --- a/dump.c +++ b/dump.c @@ -30,10 +30,8 @@ */ #include -#ifndef __linux__ #include #include -#endif #include #include #include @@ -209,7 +207,7 @@ dump_partition_entry(partition_map *entry, int digits, char *dev) int aflag = 0; #endif int pflag = 1; - u32 size; + uint32_t size; double bytes; @@ -418,8 +416,8 @@ show_data_structures(partition_map_header *map) printf("\n"); /* -u32 dpme_boot_args[32] ; -u32 dpme_reserved_3[62] ; +uint32_t dpme_boot_args[32] ; +uint32_t dpme_reserved_3[62] ; */ printf(" #: type length base " "flags (logical)\n"); diff --git a/errors.c b/errors.c index 47c9c30..af4d9b8 100644 --- a/errors.c +++ b/errors.c @@ -26,9 +26,7 @@ */ #include -#ifndef __linux__ #include -#endif #include #include diff --git a/fdisk.c b/fdisk.c deleted file mode 100644 index d77619b..0000000 --- a/fdisk.c +++ /dev/null @@ -1,1514 +0,0 @@ -/* fdisk.c -- Partition table manipulator for Linux. - * - * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk) - * - * 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 1 or - * (at your option) any later version. - * - * Before Linux version 0.95c, this program requires a kernel patch. - * - * Modified, Tue Feb 2 18:46:49 1993, faith@cs.unc.edu to better support SCSI. - * Modified, Sat Feb 27 18:11:27 1993, faith@cs.unc.edu: added extfs support. - * Modified, Sat Mar 6 10:14:12 1993, faith@cs.unc.edu: added more comments. - * Modified, Sat Mar 6 12:25:45 1993, faith@cs.unc.edu: - * Added patches from Michael Bischoff (i1041905@ws.rz.tu-bs.de - * or mbi@mo.math.nat.tu-bs.de) to fix the following problems: - * 1) Incorrect mapping of head/sector/cylinder to absolute sector - * 2) Odd sector count causes one sector to be lost - * Modified, Sat Mar 6 12:25:52 1993, faith@cs.unc.edu: improved verification. - * Modified, Sat Apr 17 15:00:00 1993, LeBlanc@mcc.ac.uk: add -s, fix -l. - * Modified, Sat Apr 24 10:00:00 1993, LeBlanc@mcc.ac.uk: fix overlap bug. - * Modified, Wed May 5 21:30:00 1993, LeBlanc@mcc.ac.uk: errors to stderr. - * Modified, Mon Mar 21 20:00:00 1994, LeBlanc@mcc.ac.uk: - * more stderr for messages, avoid division by 0, and - * give reboot message only if ioctl(fd, BLKRRPART) fails. - * Modified, Mon Apr 25 01:01:05 1994, martin@cs.unc.edu: - * 1) Added support for DOS, OS/2, ... compatibility. We should be able - * use this fdisk to partition our drives for other operating systems. - * 2) Added a print the raw data in the partition table command. - * Modified, Wed Jun 22 21:05:30 1994, faith@cs.unc.edu: - * Added/changed a few partition type names to conform to cfdisk. - * (suggested by Sujal, smpatel@wam.umd.edu) - * Modified 3/5/95 leisner@sdsp.mc.xerox.com -- on -l only open - * devices RDONLY (instead of RDWR). This allows you to - * have the disks as rw-r----- with group disk (and if you - * want is safe to setguid fdisk to disk). - * Modified Sat Mar 11 10:02 1995 with more partition types, faith@cs.unc.edu - * Modified, Thu May 4 01:11:45 1995, esr@snark.thyrsus.com: - * It's user-interface cleanup time. - * Actual error messages for out-of-bounds values (what a concept!). - * Enable read-only access to partition table for learners. - * Smart defaults for most numeric prompts. - * Fixed a bug preventing a partition from crossing cylinder 8064, aeb, 950801. - * Read partition table twice to avoid kernel bug - * (from Daniel Quinlan ), Tue Sep 26 10:25:28 1995 - * Modified, Sat Jul 1 23:43:16 MET DST 1995, fasten@cs.bonn.edu: - * editor for NetBSD/i386 (and Linux/Alpha?) disklabels. - * Tue Sep 26 17:07:54 1995: More patches from aeb. Fix segfaults, all >4GB. - * Don't destroy random data if extd partition starts past 4GB, aeb, 950818. - * Don't segfault on bad partition created by previous fdisk. - * Modified, Fri Jul 14 11:13:35 MET DST 1996, jj@sunsite.mff.cuni.cz: - * editor for Sun disklabels. - * Modified, Wed Jul 3 10:14:17 MET DST 1996, jj@sunsite.mff.cuni.cz: - * support for sun floppies - * Modified, Fri Dec 20 15:05:56 PST 1996, eryk@apple.com: - * got rid of sun stuff, but kept byte order fixes - * - */ - - -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -typedef unsigned short kdev_t; /* BAD hack; kdev_t is not exported */ - -#include "kernel-defs.h" - -#include "fdisk.h" - -#define hex_val(c) ({ \ - char _c = (c); \ - isdigit(_c) ? _c - '0' : \ - tolower(_c) + 10 - 'a'; \ - }) - - -#define VERSION "2.1 (>4GB)" - -#define DEFAULT_DEVICE "/dev/hda" -#define ALTERNATE_DEVICE "/dev/sda" -#define LINE_LENGTH 80 -#define MAXIMUM_PARTS 60 -#define PART_TABLE_FLAG 0xaa55 -#define table_check(b) ((unsigned short *)((b) + 0x1fe)) -#define offset(b, n) ((struct partition *)((b) + 0x1be + \ - (n) * sizeof(struct partition))) -#define sector(s) ((s) & 0x3f) -#define cylinder(s, c) ((c) | (((s) & 0xc0) << 2)) - -#define calculate(h,s,c) (sector(s) - 1 + sectors * \ - ((h) + heads * cylinder(s,c))) -#define set_hsc(h,s,c,sector) { \ - s = sector % sectors + 1; \ - sector /= sectors; \ - h = sector % heads; \ - sector /= heads; \ - c = sector & 0xff; \ - s |= (sector >> 2) & 0xc0; \ - } - -#define ACTIVE_FLAG 0x80 -#define EXTENDED 5 - -#define LINUX_PARTITION 0x81 -#define LINUX_SWAP 0x82 -#define LINUX_NATIVE 0x83 - -/* normally O_RDWR, -l option gives O_RDONLY */ -static int type_open = O_RDWR; - -char *disk_device = DEFAULT_DEVICE, /* hda, unless specified */ - *line_ptr, /* interactive input */ - line_buffer[LINE_LENGTH], - changed[MAXIMUM_PARTS], /* marks changed buffers */ - buffer[SECTOR_SIZE], /* first four partitions */ - *buffers[MAXIMUM_PARTS] /* pointers to buffers */ - = {buffer, buffer, buffer, buffer}; - -int fd, /* the disk */ - ext_index, /* the prime extended partition */ - listing = 0, /* no aborts for fdisk -l */ - dos_compatible_flag = ~0, - partitions = 4; /* maximum partition + 1 */ - -uint heads, - sectors, - cylinders, - sector_offset = 1, - display_factor = 1, /* in units/sector */ - unit_flag = 1, - full_bits = 0, /* 1024 cylinders in sectors */ - extended_offset = 0, /* offset of link pointers */ - offsets[MAXIMUM_PARTS] = {0, 0, 0, 0}; - -struct partition *part_table[MAXIMUM_PARTS] /* partitions */ - = {offset(buffer, 0), offset(buffer, 1), - offset(buffer, 2), offset(buffer, 3)}, - *ext_pointers[MAXIMUM_PARTS] /* link pointers */ - = {NULL, NULL, NULL, NULL}; - -struct systypes sys_types[] = { /* struct systypes in fdisk.h *//* bf */ - {0, "Empty"}, - {1, "DOS 12-bit FAT"}, - {2, "XENIX root"}, - {3, "XENIX usr"}, - {4, "DOS 16-bit <32M"}, - {EXTENDED, "Extended"}, - {6, "DOS 16-bit >=32M"}, - {7, "OS/2 HPFS"}, /* or QNX? */ - {8, "AIX"}, - {9, "AIX bootable"}, - {10, "OS/2 Boot Manager"}, - {0x40, "Venix 80286"}, - {0x51, "Novell?"}, - {0x52, "Microport"}, /* or CPM? */ - {0x63, "GNU HURD"}, /* or System V/386? */ - {0x64, "Novell Netware 286"}, - {0x65, "Novell Netware 386"}, - {0x75, "PC/IX"}, - {0x80, "Old MINIX"}, /* Minix 1.4a and earlier */ - - {LINUX_PARTITION, "Linux/MINIX"}, /* Minix 1.4b and later */ - {LINUX_SWAP, "Linux swap"}, - {LINUX_NATIVE, "Linux native"}, - - {0x93, "Amoeba"}, - {0x94, "Amoeba BBT"}, /* (bad block table) */ - {0xa5, "BSD/386"}, - {0xb7, "BSDI fs"}, - {0xb8, "BSDI swap"}, - {0xc7, "Syrinx"}, - {0xdb, "CP/M"}, /* or Concurrent DOS? */ - {0xe1, "DOS access"}, - {0xe3, "DOS R/O"}, - {0xf2, "DOS secondary"}, - {0xff, "BBT"} /* (bad track table) */ - }; - -int nsys_types = sizeof (sys_types) / sizeof (struct systypes); /* bf */ - -uint read_int(uint low, uint dflt, uint high, enum offset base, char *mesg); -char read_char(char *mesg); - -jmp_buf listingbuf; - -inline unsigned short __swap16(unsigned short x) { - return (((__u16)(x) & 0xFF) << 8) | (((__u16)(x) & 0xFF00) >> 8); -} -inline __u32 __swap32(__u32 x) { - return (((__u32)(x) & 0xFF) << 24) | (((__u32)(x) & 0xFF00) << 8) | (((__u32)(x) & 0xFF0000) >> 8) | (((__u32)(x) & 0xFF000000) >> 24); -} - -void fatal(enum failure why) -{ - char error[LINE_LENGTH], - *message = error; - - if (listing) { - close(fd); - longjmp(listingbuf, 1); - } - - switch (why) { - case usage: message = - "Usage: fdisk [-l] [-v] [-s /dev/hdxn] [/dev/hdx]\n"; - break; - case unable_to_open: - sprintf(error, "Unable to open %s\n", disk_device); - break; - case unable_to_read: - sprintf(error, "Unable to read %s\n", disk_device); - break; - case unable_to_seek: - sprintf(error, "Unable to seek on %s\n", disk_device); - break; - case unable_to_write: - sprintf(error, "Unable to write %s\n", disk_device); - break; - case out_of_memory: - message = "Unable to allocate any more memory\n"; - break; - default: message = "Fatal error\n"; - } - - fputc('\n', stderr); - fputs(message, stderr); - exit(1); -} - -void menu(void) -{ - puts("Command action\n" - " a toggle a bootable flag\n" - " b edit bsd disklabel\n" /* bf */ - " c toggle the dos compatiblity flag\n" - " d delete a partition\n" - " l list known partition types\n" - " m print this menu\n" - " n add a new partition\n" - " p print the partition table\n" - " q quit without saving changes\n" - " t change a partition's system id\n" - " u change display/entry units\n" - " v verify the partition table\n" - " w write table to disk and exit\n" - " x extra functionality (experts only)" - ); -} - -void xmenu(void) -{ - puts("Command action\n" - " b move beginning of data in a partition\n" - " c change number of cylinders\n" - " d print the raw data in the partition table\n" - " e list extended partitions\n" - " h change number of heads\n" - " m print this menu\n" - " p print the partition table\n" - " q quit without saving changes\n" - " r return to main menu\n" - " s change number of sectors\n" - " v verify the partition table\n" - " w write table to disk and exit" - ); -} - -char *partition_type(unsigned char type) -{ - int high = sizeof(sys_types) / sizeof(struct systypes), - low = 0, mid; - uint tmp; - - while (high >= low) { - mid = (high + low) >> 1; - if ((tmp = sys_types[mid].index) == type) - return sys_types[mid].name; - else if (tmp < type) - low = mid + 1; - else high = mid - 1; - } - return NULL; -} - -/* added parameters to list_types() so fdisklabel - can pass another list of types. -*//* bf */ -void list_types(struct systypes *sys, int size) -{ - uint last[4], done = 0, next = 0; - int i; - - for (i = 3; i >= 0; i--) - last[3 - i] = done += (size + i - done) / (i + 1); - i = done = 0; - - do { - printf("%c%2x %-15.15s", i ? ' ' : '\n', - sys[next].index, sys[next].name); - next = last[i++] + done; - if (i > 3 || next >= last[i]) { - i = 0; - next = ++done; - } - } while (done < last[0]); - putchar('\n'); -} - -void clear_partition(struct partition *p) -{ - p->boot_ind = 0; - p->head = 0; - p->sector = 0; - p->cyl = 0; - p->sys_ind = 0; - p->end_head = 0; - p->end_sector = 0; - p->end_cyl = 0; - p->start_sect = 0; - p->nr_sects = 0; -} - -void set_partition(int i, struct partition *p, uint start, uint stop, - int sys, uint offset) -{ - p->boot_ind = 0; - p->sys_ind = sys; - p->start_sect = SWAP32(start - offset); - p->nr_sects = SWAP32(stop - start + 1); - if (dos_compatible_flag && (start/(sectors*heads) > 1023)) - start = heads*sectors*1024 - 1; - set_hsc(p->head, p->sector, p->cyl, start); - if (dos_compatible_flag && (stop/(sectors*heads) > 1023)) - stop = heads*sectors*1024 - 1; - set_hsc(p->end_head, p->end_sector, p->end_cyl, stop); - changed[i] = 1; -} - -int test_c(char **m, char *mesg) -{ - int val = 0; - if (!*m) - fprintf(stderr, "You must set"); - else { - fprintf(stderr, " %s", *m); - val = 1; - } - *m = mesg; - return val; -} - -int warn_geometry(void) -{ - char *m = NULL; - int prev = 0; - if (!heads) - prev = test_c(&m, "heads"); - if (!sectors) - prev = test_c(&m, "sectors"); - if (!cylinders) - prev = test_c(&m, "cylinders"); - if (!m) - return 0; - fprintf(stderr, - "%s%s.\nYou can do this from the extra functions menu.\n", - prev ? " and " : " ", m); - return 1; -} - -uint rounded(uint calcul, uint start) -{ - uint i; - if (!full_bits) - return calcul; - while ((i = calcul + full_bits) <= start) - calcul = i; - return calcul; -} - -void update_units(void) -{ - full_bits = 1024 * heads * sectors; - if (unit_flag && full_bits) - display_factor = full_bits >> 10; - else display_factor = 1; -} - -void warn_cylinders(void) -{ - update_units(); - if (cylinders > 1024) - fprintf(stderr, "The number of cylinders for this disk is " - "set to %d.\nThis is larger than 1024, and may cause " - "problems with:\n" - "1) software that runs at boot time (e.g., LILO)\n" - "2) booting and partitioning software form other OSs\n" - " (e.g., DOS FDISK, OS/2 FDISK)\n", - cylinders); -} - -void read_extended(struct partition *p) -{ - int i; - struct partition *q; - - ext_pointers[ext_index] = part_table[ext_index]; - if (!p->start_sect) - fprintf(stderr, "Bad offset in primary extended partition\n"); - else while (p->sys_ind == EXTENDED) { - if (partitions >= MAXIMUM_PARTS) { - fprintf(stderr, - "Warning: deleting partitions after %d\n", - partitions); - clear_partition(ext_pointers[partitions - 1]); - changed[partitions - 1] = 1; - return; - } - offsets[partitions] = extended_offset + SWAP32(p->start_sect); - if (!extended_offset) - extended_offset = SWAP32(p->start_sect); - if (lseek64(fd, (loff_t)offsets[partitions] - * SECTOR_SIZE, SEEK_SET) < 0) - fatal(unable_to_seek); - if (!(buffers[partitions] = (char *) malloc(SECTOR_SIZE))) - fatal(out_of_memory); - if (SECTOR_SIZE != read(fd, buffers[partitions], SECTOR_SIZE)) - fatal(unable_to_read); - part_table[partitions] = ext_pointers[partitions] = NULL; - q = p = offset(buffers[partitions], 0); - for (i = 0; i < 4; i++, p++) { - if (p->sys_ind == EXTENDED) { - if (ext_pointers[partitions]) - fprintf(stderr, "Warning: extra link " - "pointer in partition table " - "%d\n", partitions + 1); - else - ext_pointers[partitions] = p; - } else if (p->sys_ind) { - if (part_table[partitions]) - fprintf(stderr, - "Warning: ignoring extra data " - "in partition table %d\n", - partitions + 1); - else - part_table[partitions] = p; - } - } - if (!part_table[partitions]) { - if (q != ext_pointers[partitions]) - part_table[partitions] = q; - else part_table[partitions] = q + 1; - } - if (!ext_pointers[partitions]) { - if (q != part_table[partitions]) - ext_pointers[partitions] = q; - else ext_pointers[partitions] = q + 1; - } - p = ext_pointers[partitions++]; - } -} - -void get_boot(void) -{ - int i; - struct hd_geometry geometry; - - partitions = 4; - if ((fd = open(disk_device, type_open)) < 0) - { - if ((fd = open(disk_device, O_RDONLY)) < 0) - fatal(unable_to_open); - else - printf("You will not be able to write the partition table.\n"); - } - if (SECTOR_SIZE != read(fd, buffer, SECTOR_SIZE)) - fatal(unable_to_read); -#ifdef HDIO_REQ - if (!ioctl(fd, HDIO_REQ, &geometry)) { -#else - if (!ioctl(fd, HDIO_GETGEO, &geometry)) { -#endif - heads = geometry.heads; - sectors = geometry.sectors; - cylinders = geometry.cylinders; - if (dos_compatible_flag) - sector_offset = sectors; - warn_cylinders(); - } - else update_units(); - warn_geometry(); - - for (i = 0; i < 4; i++) - if(part_table[i]->sys_ind == EXTENDED) { - if (partitions != 4) - fprintf(stderr, "Ignoring extra extended " - "partition %d\n", i + 1); - else read_extended(part_table[ext_index = i]); - } - - for (i = 3; i < partitions; i++) - if (SWAP16(*table_check(buffers[i])) != PART_TABLE_FLAG) { - fprintf(stderr, "Warning: invalid flag %04x of parti" - "tion table %d will be corrected by w(rite)\n", - SWAP16(*table_check(buffers[i])), i + 1); - changed[i] = 1; - } -} - -int read_line(void) -{ - if (!fgets(line_buffer, LINE_LENGTH, stdin)) - return 0; - line_ptr = line_buffer; - while (*line_ptr && !isgraph(*line_ptr)) - line_ptr++; - return *line_ptr; -} - -char read_char(char *mesg) -{ - do - fputs(mesg, stdout); - while (!read_line()); - return *line_ptr; -} - -char read_chars(char *mesg) -{ - fputs(mesg, stdout); - if (!read_line()) { - *line_ptr = '\n'; - return '\n'; - } else return *line_ptr; -} - -/* new function *//* bf */ -int read_hex(struct systypes *sys, int size) -{ - int hex; - - while (1) - { - read_char("Hex code (type L to list codes): "); - if (tolower(*line_ptr) == 'l') - list_types(sys, size); - else if (isxdigit (*line_ptr)) - { - hex = 0; - do - hex = hex << 4 | hex_val(*line_ptr++); - while (isxdigit(*line_ptr)); - return hex; - } - } -} - - -uint read_int(uint low, uint dflt, uint high, enum offset base, char *mesg) -{ - uint i, use_default = 1; - char ms[70]; - - switch(base) { - case lower: - sprintf(ms, "%s ([%d]-%d): ", mesg, low, high); - break; - case upper: - sprintf(ms, "%s (%d-[%d]): ", mesg, low, high); - break; - case deflt: - sprintf(ms, "%s (%d-[%d]-%d): ", mesg, low, dflt, high); - break; - default: - sprintf(ms, "%s (%d-%d): ", mesg, low, high); - break; - } - - while (1) { - if (base == deflt) { - while (read_chars(ms) != '\n' && !isdigit(*line_ptr) - && (*line_ptr != '-' && *line_ptr != '+')) - continue; - if (*line_ptr == '\n') - return dflt; - } else { - while (!isdigit(read_char(ms)) - && (*line_ptr != '-' && *line_ptr != '+')) - continue; - } - if (*line_ptr == '+' || *line_ptr == '-') { - if (*line_ptr == '+') - ++line_ptr; - i = atoi(line_ptr); - while (isdigit(*line_ptr)) - { - line_ptr++; - use_default = 0; - } - switch (*line_ptr) { - case 'c': - case 'C': if (!unit_flag) - i *= heads * sectors; - break; - case 'k': - case 'K': i *= 2; - i /= display_factor; - break; - case 'm': - case 'M': i *= 2048; - i /= display_factor; - break; - default: break; - } - switch(base) { - case lower: i += low; break; - case upper: i += high; break; - case deflt: i += dflt; break; - default: break; - } - } - else - { - i = atoi(line_ptr); - while (isdigit(*line_ptr)) - { - line_ptr++; - use_default = 0; - } - } - if (use_default) - printf("Using default value %d\n", i = dflt); - if (i >= low && i <= high) - break; - else - printf("Value out of range.\n"); - } - return i; -} - -int get_partition(int warn, int max) -{ - /* - * try to pick a default least likely to do damage, - * in case luser just types a newline - */ - int i = read_int(1, max, max, ignore, "Partition number") - 1; - - if (warn && !part_table[i]->sys_ind) - fprintf(stderr, "Warning: partition %d has empty type\n", - i + 1); - return i; -} - -char *const str_units(void) -{ - return unit_flag ? "cylinder" : "sector"; -} - -void change_units(void) -{ - if ((unit_flag = !unit_flag)) - display_factor = 1; - else display_factor = heads * sectors; - update_units(); - printf("Changing display/entry units to %ss\n", - str_units()); -} - -void toggle_active(int i) -{ - struct partition *p = part_table[i]; - - if (p->sys_ind == EXTENDED && !p->boot_ind) - fprintf(stderr, - "WARNING: Partition %d is an extended partition\n", - i + 1); - if (p->boot_ind) - p->boot_ind = 0; - else p->boot_ind = ACTIVE_FLAG; - changed[i] = 1; -} - -void toggle_dos(void) -{ - dos_compatible_flag = ~dos_compatible_flag; - printf("DOS Compatibility flag is "); - if (dos_compatible_flag) - sector_offset = sectors; - else { - sector_offset = 1; - printf("not "); - } - printf("set\n"); -} - -void delete_partition(int i) -{ - struct partition *p = part_table[i], *q = ext_pointers[i]; - -/* Note that for the fifth partition (i == 4) we don't actually - * decrement partitions. - */ - - if (warn_geometry()) - return; - changed[i] = 1; - if (i < 4) { - if (p->sys_ind == EXTENDED && i == ext_index) { - while (partitions > 4) - free(buffers[--partitions]); - ext_pointers[ext_index] = NULL; - extended_offset = 0; - } - clear_partition(p); - } - else if (!q->sys_ind && i > 4) { - free(buffers[--partitions]); - clear_partition(ext_pointers[--i]); - } - else if (i > 3) { - if (i > 4) { - p = ext_pointers[i - 1]; - p->boot_ind = 0; - p->head = q->head; - p->sector = q->sector; - p->cyl = q->cyl; - p->sys_ind = EXTENDED; - p->end_head = q->end_head; - p->end_sector = q->end_sector; - p->end_cyl = q->end_cyl; - p->start_sect = q->start_sect; - p->nr_sects = q->nr_sects; - changed[i - 1] = 1; - } - else { - if(part_table[5]) /* prevent SEGFAULT */ - part_table[5]->start_sect = - SWAP32( - SWAP32(part_table[5]->start_sect) + - offsets[5] - extended_offset - ); - offsets[5] = extended_offset; - changed[5] = 1; - } - if (partitions > 5) { - partitions--; - free(buffers[i]); - while (i < partitions) { - changed[i] = changed[i + 1]; - buffers[i] = buffers[i + 1]; - offsets[i] = offsets[i + 1]; - part_table[i] = part_table[i + 1]; - ext_pointers[i] = ext_pointers[i + 1]; - i++; - } - } - else - clear_partition(part_table[i]); - } -} - -void change_sysid(void) -{ - char *temp; - int i = get_partition(0, partitions), sys, origsys; - struct partition *p = part_table[i]; - - sys = p->sys_ind; - origsys = sys; - if (sys == EXTENDED) - printf("Partition %d is extended. Delete it\n", i + 1); - else if (!sys) - printf("Partition %d does not exist yet!\n", i + 1); - else while (1) { -#if 0 - read_char("Hex code (type L to list codes): "); - if (tolower(*line_ptr) == 'l') - list_types(); - else if (isxdigit(*line_ptr)) { - sys = 0; - do - sys = sys << 4 | hex_val(*line_ptr++); - while (isxdigit(*line_ptr)); -#endif - /* The above code has been moved to read_hex () - to avoid having the same code in fdisklabel. - *//* bf */ - sys = read_hex (sys_types, nsys_types); /* bf */ - - if (!sys) { - delete_partition(i); - break; - } - else if (sys == EXTENDED) { - printf("You may not change a partition " - "to be an extended partition\n"); - break; - } - else if (sys < 256) { - if (sys == origsys) - break; - part_table[i]->sys_ind = sys; - printf ("Changed system type of partition %d " - "to %x (%s)\n", i + 1, sys, - (temp = partition_type(sys)) ? temp : - "Unknown"); - changed[i] = 1; - break; - } -#if 0 /* see above *//* bf */ - } -#endif - } -} - -/* check_consistency() and long2chs() added Sat Mar 6 12:28:16 1993, - * faith@cs.unc.edu, based on code fragments from pfdisk by Gordon W. Ross, - * Jan. 1990 (version 1.2.1 by Gordon W. Ross Aug. 1990; Modified by S. - * Lubkin Oct. 1991). */ - -static void long2chs(ulong ls, uint *c, uint *h, uint *s) -{ - int spc = heads * sectors; - - *c = ls / spc; - ls = ls % spc; - *h = ls / sectors; - *s = ls % sectors + 1; /* sectors count from 1 */ -} - -static void check_consistency(struct partition *p, int partition) -{ - uint pbc, pbh, pbs; /* physical beginning c, h, s */ - uint pec, peh, pes; /* physical ending c, h, s */ - uint lbc, lbh, lbs; /* logical beginning c, h, s */ - uint lec, leh, les; /* logical ending c, h, s */ - - if (!heads || !sectors || (partition >= 4)) - return; /* do not check extended partitions */ - -/* physical beginning c, h, s */ - pbc = (p->cyl & 0xff) | ((p->sector << 2) & 0x300); - pbh = p->head; - pbs = p->sector & 0x3f; - -/* physical ending c, h, s */ - pec = (p->end_cyl & 0xff) | ((p->end_sector << 2) & 0x300); - peh = p->end_head; - pes = p->end_sector & 0x3f; - -/* compute logical beginning (c, h, s) */ - long2chs(SWAP32(p->start_sect), &lbc, &lbh, &lbs); - -/* compute logical ending (c, h, s) */ - long2chs(SWAP32(p->start_sect) + SWAP32(p->nr_sects) - 1, &lec, &leh, &les); - -/* Same physical / logical beginning? */ - if (cylinders <= 1024 && (pbc != lbc || pbh != lbh || pbs != lbs)) { - printf("Partition %d has different physical/logical " - "beginnings (non-Linux?):\n", partition + 1); - printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs); - printf("logical=(%d, %d, %d)\n",lbc, lbh, lbs); - } - -/* Same physical / logical ending? */ - if (cylinders <= 1024 && (pec != lec || peh != leh || pes != les)) { - printf("Partition %d has different physical/logical " - "endings:\n", partition + 1); - printf(" phys=(%d, %d, %d) ", pec, peh, pes); - printf("logical=(%d, %d, %d)\n",lec, leh, les); - } - -#if 0 -/* Beginning on cylinder boundary? */ - if (pbh != !pbc || pbs != 1) { - printf("Partition %i does not start on cylinder " - "boundary:\n", partition + 1); - printf(" phys=(%d, %d, %d) ", pbc, pbh, pbs); - printf("should be (%d, %d, 1)\n", pbc, !pbc); - } -#endif - -/* Ending on cylinder boundary? */ - if (peh != (heads - 1) || pes != sectors) { - printf("Partition %i does not end on cylinder boundary:\n", - partition + 1); - printf(" phys=(%d, %d, %d) ", pec, peh, pes); - printf("should be (%d, %d, %d)\n", - pec, heads - 1, sectors); - } -} - -void list_table(void) -{ - struct partition *p; - char *type; - int i, w = strlen(disk_device); - - printf("\nDisk %s: %d heads, %d sectors, %d cylinders\nUnits = " - "%ss of %d * 512 bytes\n\n", disk_device, heads, sectors, - cylinders, str_units(), display_factor); - if (w < 5) - w = 5; - printf("%*s Boot Begin Start End Blocks Id System\n", - w + 1, "Device"); - for (i = 0 ; i < partitions; i++) - if ((p = part_table[i])->sys_ind) { - printf("%*s%-2d %c%9d%9d%9d%9d%c %2x %s\n", w, -/* device */ disk_device, i + 1, -/* boot flag */ !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG - ? '*' : '?', -/* begin */ cround(rounded( calculate(p->head, p->sector, p->cyl), - SWAP32(p->start_sect + offsets[i]))), -/* start */ cround(SWAP32(p->start_sect) + offsets[i]), -/* end */ cround(SWAP32(p->start_sect) + offsets[i] + SWAP32(p->nr_sects) - - (p->nr_sects ? 1: 0)), -/* odd flag on end */ SWAP32(p->nr_sects) / 2,SWAP32(p->nr_sects) & 1 ? '+' : ' ', -/* type id */ p->sys_ind, -/* type name */ (type = partition_type(p->sys_ind)) ? - type : "Unknown"); - check_consistency(p, i); - } - -} - -void x_list_table(int extend) -{ - struct partition *p, **q; - int i; - - if (extend) - q = ext_pointers; - else - q = part_table; - printf("\nDisk %s: %d heads, %d sectors, %d cylinders\n\n", - disk_device, heads, sectors, cylinders); - printf("Nr AF Hd Sec Cyl Hd Sec Cyl Start Size ID\n"); - for (i = 0 ; i < partitions; i++) - if ((p = q[i])) { - printf("%2d %02x%4d%4d%5d%4d%4d%5d%8d%8d %02x\n", - i + 1, p->boot_ind, p->head, - sector(p->sector), - cylinder(p->sector, p->cyl), p->end_head, - sector(p->end_sector), - cylinder(p->end_sector, p->end_cyl), - SWAP32(p->start_sect), SWAP32(p->nr_sects), p->sys_ind); - if (p->sys_ind) - check_consistency(p, i); - } -} - -void check_bounds(uint *first, uint *last) -{ - int i; - uint max = 0xffffffff; /* used to be 256 * 63 * 1024 - but that made it impossible to add a - partition crossing cylinder 8064 */ - struct partition *p = part_table[0]; - - for (i = 0; i < partitions; p = part_table[++i]) - if (!p->sys_ind || p->sys_ind == EXTENDED) { - first[i] = max; - last[i] = 0; - } - else { - first[i] = rounded(calculate(p->head, p->sector, - p->cyl), SWAP32(p->start_sect) + offsets[i]); - last[i] = SWAP32(p->start_sect) + offsets[i] + SWAP32(p->nr_sects) - 1; - } -} - -void check(int n, uint h, uint s, uint c, uint start) -{ - uint total, real_s, real_c, i; - - real_s = sector(s) - 1; - real_c = cylinder(s, c); - total = (real_c * sectors + real_s) * heads + h; - if (full_bits) - while ((i = total + full_bits) <= start) { - real_c += 1024; - total = i; - } - if (!total) - fprintf(stderr, "Warning: partition %d contains sector 0\n", n); - if (h >= heads) - fprintf(stderr, - "Partition %d: head %d greater than maximum %d\n", - n, h + 1, heads); - if (real_s >= sectors) - fprintf(stderr, "Partition %d: sector %d greater than " - "maximum %d\n", n, s, sectors); - if (real_c >= cylinders) - fprintf(stderr, "Partitions %d: cylinder %d greater than " - "maximum %d\n", n, real_c + 1, cylinders); - if (cylinders <= 1024 && start != total) - fprintf(stderr, - "Partition %d: previous sectors %d disagrees with " - "total %d\n", n, start, total); -} - - -void verify(void) -{ - int i, j; - uint total = 1, - first[partitions], last[partitions]; - struct partition *p = part_table[0]; - - if (warn_geometry()) - return; - - check_bounds(first, last); - for (i = 0; i < partitions; p = part_table[++i]) - if (p->sys_ind && (p->sys_ind != EXTENDED)) { - check_consistency(p, i); - if (SWAP32(p->start_sect) + offsets[i] < first[i]) - printf("Warning: bad start-of-data in " - "partition %d\n", i + 1); - check(i + 1, p->end_head, p->end_sector, p->end_cyl, - last[i]); - total += last[i] + 1 - first[i]; - for (j = 0; j < i; j++) - if ((first[i] >= first[j] && first[i] <= last[j]) - || (last[i] <= last[j] && - last[i] >= first[j])) { - printf("Warning: partition %d overlaps " - "partition %d.\n", j + 1, i + 1); - total += first[i] >= first[j] ? - first[i] : first[j]; - total -= last[i] <= last[j] ? - last[i] : last[j]; - } - } - - if (extended_offset) { - uint e_last = SWAP32(part_table[ext_index]->start_sect) + - SWAP32(part_table[ext_index]->nr_sects) - 1; - - for (p = part_table[i = 4]; i < partitions; - p = part_table[++i]) { - total++; - if (!p->sys_ind) { - if (i != 4 || i + 1 < partitions) - printf("Warning: partition %d " - "is empty\n", i + 1); - } - else if (first[i] < extended_offset || - last[i] > e_last) - printf("Logical partition %d not entirely in " - "partition %d\n", i + 1, ext_index + 1); - } - } - - if (total > heads * sectors * cylinders) - printf("Total allocated sectors %d greater than the maximum " - "%d\n", total, heads * sectors * cylinders); - else if ((total = heads * sectors * cylinders - total)) - printf("%d unallocated sectors\n", total); -} - -static void add_partition(int n, int sys) -{ - char mesg[48]; - int i, read = 0; - struct partition *p = part_table[n], *q = part_table[ext_index]; - uint start, stop = 0, limit, temp, - first[partitions], last[partitions]; - - if (p->sys_ind) { - printf("Partition %d is already defined. Delete " - "it before re-adding it.\n", n + 1); - return; - } - check_bounds(first, last); - if (n < 4) { - start = sector_offset; - limit = heads * sectors * cylinders - 1; - if (extended_offset) { - first[ext_index] = extended_offset; - last[ext_index] = SWAP32(q->start_sect) + SWAP32(q->nr_sects) - 1; - } - } - else { - start = extended_offset + sector_offset; - limit = SWAP32(q->start_sect) + SWAP32(q->nr_sects) - 1; - } - if (unit_flag) - for (i = 0; i < partitions; i++) - first[i] = (cround(first[i]) - 1) * display_factor; - - sprintf(mesg, "First %s", str_units()); - do { - temp = start; - for (i = 0; i < partitions; i++) { - if (start == offsets[i]) - start += sector_offset; - if (start >= first[i] && start <= last[i]) { - if (n < 4) - start = last[i] + 1; - else - start = last[i] + sector_offset; - } - } - if (start > limit) - break; - if (start != temp && read) { - printf("Sector %d is already allocated\n", temp); - temp = start = stop; - read = 0; - } - if (!read && start == temp) { - uint i; - i = (stop = start) + (n > 4); - start = read_int(cround(i), cround(i), cround(limit), - ignore, mesg); - if (unit_flag) { - start = (start - 1) * display_factor; - if (start < i) start = i; - } - read = 1; - } - } while (start != temp || !read); - if (n > 4) /* NOT for fifth partition */ - offsets[n] = start - sector_offset; - - for (i = 0; i < partitions; i++) { - if (start < offsets[i] && limit >= offsets[i]) - limit = offsets[i] - 1; - if (start < first[i] && limit >= first[i]) - limit = first[i] - 1; - } - if (start > limit) { - printf("No free sectors available\n"); - if (n > 4) { - free(buffers[n]); - partitions--; - } - return; - } - if (cround(start) == cround(limit)) - stop = start; - else { - sprintf(mesg, "Last %s or +size or +sizeM or +sizeK", - str_units()); - stop = read_int(cround(start), cround(limit), cround(limit), - lower, mesg); - if (unit_flag) { - stop = stop * display_factor - 1; - if (stop >limit) - stop = limit; - } - } - - set_partition(n, p, start, stop, sys, offsets[n]); - - if (sys == EXTENDED) { - ext_index = n; - offsets[4] = extended_offset = start; - ext_pointers[n] = p; - if (!(buffers[4] = calloc(1, SECTOR_SIZE))) - fatal(out_of_memory); - part_table[4] = offset(buffers[4], 0); - ext_pointers[4] = part_table[4] + 1; - changed[4] = 1; - partitions = 5; - } - else { - if (n > 4) - set_partition(n - 1, ext_pointers[n - 1], - start - sector_offset, stop, EXTENDED, - extended_offset); -#if 0 - if ((limit = SWAP32(p->nr_sects)) & 1) - printf("Warning: partition %d has an odd " - "number of sectors.\n", n + 1); -#endif - } -} - -void add_logical(void) -{ - if (partitions > 5 || part_table[4]->sys_ind) { - if (!(buffers[partitions] = calloc(1, SECTOR_SIZE))) - fatal(out_of_memory); - part_table[partitions] = offset(buffers[partitions], 0); - ext_pointers[partitions] = part_table[partitions] + 1; - offsets[partitions] = 0; - partitions++; - } - add_partition(partitions - 1, LINUX_NATIVE); -} - -void new_partition(void) -{ - int i, free_primary = 0; - - if (warn_geometry()) - return; - if (partitions >= MAXIMUM_PARTS) { - printf("The maximum number of partitions has been created\n"); - return; - } - - for (i = 0; i < 4; i++) - free_primary += !part_table[i]->sys_ind; - if (!free_primary) - if (extended_offset) - add_logical(); - else - printf("You must delete some partition and add " - "an extended partition first\n"); - else { - char c, line[LINE_LENGTH]; - sprintf(line, "Command action\n %s\n p primary " - "partition (1-4)\n", extended_offset ? - "l logical (5 or over)" : "e extended"); - while (1) - if ((c = tolower(read_char(line))) == 'p') { - add_partition(get_partition(0, 4), - LINUX_NATIVE); - return; - } - else if (c == 'l' && extended_offset) { - add_logical(); - return; - } - else if (c == 'e' && !extended_offset) { - add_partition(get_partition(0, 4), - EXTENDED); - return; - } - else - printf("Invalid partition number " - "for type `%c'\n", c); - - } -} - -void write_table(void) -{ - int i, error = 0; - - changed[3] = changed[0] || changed[1] || changed[2] || changed[3]; - for (i = 3; i < partitions; i++) - if (changed[i]) { - *table_check(buffers[i]) = SWAP16(PART_TABLE_FLAG); - if (lseek64(fd, (loff_t)offsets[i] - * SECTOR_SIZE, SEEK_SET) < 0) - fatal(unable_to_seek); - if (write(fd, buffers[i], SECTOR_SIZE) != SECTOR_SIZE) - fatal(unable_to_write); - } - - printf("The partition table has been altered!\n\n"); - - printf("Calling ioctl() to re-read partition table.\n" - "(Reboot to ensure the partition table has been updated.)\n"); - sync(); - sleep(2); - if ((i = ioctl(fd, BLKRRPART))) { - error = errno; - } else { - /* some kernel versions (1.2.x) seem to have trouble - rereading the partition table, but if asked to do it - twice, the second time works. - biro@yggdrasil.com */ - sync(); - sleep(2); - if((i = ioctl(fd, BLKRRPART))) - error = errno; - } - - close(fd); - - printf("Syncing disks.\n"); - sync(); - sleep(4); /* for sync() */ - - if (i < 0) - printf("Re-read table failed with error %d: %s.\nReboot your " - "system to ensure the partition table is updated.\n", - error, strerror(error)); - - printf( "\nWARNING: If you have created or modified any DOS 6.x\n" - "partitions, please see the fdisk manual page for additional\n" - "information.\n" ); - - exit(0); -} - -#define MAX_PER_LINE 16 -void print_buffer(char buffer[]) -{ - int i, - l; - - for (i = 0, l = 0; i < SECTOR_SIZE; i++, l++) { - if (l == 0) - printf("0x%03X:", i); - printf(" %02X", (unsigned char) buffer[i]); - if (l == MAX_PER_LINE - 1) { - printf("\n"); - l = -1; - } - } - if (l > 0) - printf("\n"); - printf("\n"); -} - -void print_raw(void) -{ - int i; - - printf("Device: %s\n", disk_device); - for (i = 3; i < partitions; i++) - print_buffer(buffers[i]); -} - -void move_begin(int i) -{ - struct partition *p = part_table[i]; - uint new, first; - - if (warn_geometry()) - return; - if (!p->sys_ind || !p->nr_sects || p->sys_ind == EXTENDED) { - printf("Partition %d has no data area\n", i + 1); - return; - } - first = rounded(calculate(p->head, p->sector, p->cyl), SWAP32(p->start_sect) + - offsets[i]); - new = read_int(first, first, - SWAP32(p->start_sect) + SWAP32(p->nr_sects) + offsets[i] - 1, - lower, "New beginning of data") - offsets[i]; - - if (new != SWAP32(p->nr_sects)) { - first = SWAP32(p->nr_sects) + SWAP32(p->start_sect) - new; - p->nr_sects = SWAP32(first); - p->start_sect = SWAP32(new); - changed[i] = 1; - } -} - -void xselect(void) -{ - while(1) { - putchar('\n'); - switch (tolower(read_char("Expert command (m for help): "))) { - case 'b': move_begin(get_partition(0, partitions)); - break; - case 'c': cylinders = read_int(1, cylinders, 65535, - deflt, "Number of cylinders"); - warn_cylinders(); - break; - case 'd': print_raw(); - break; - case 'e': x_list_table(1); - break; - case 'h': heads = read_int(1, heads, 256, deflt, - "Number of heads"); - update_units(); - break; - case 'p': x_list_table(0); - break; - case 'q': close(fd); - exit(0); - case 'r': return; - case 's': sectors = read_int(1, sectors, 63, deflt, - "Number of sectors"); - if (dos_compatible_flag) { - sector_offset = sectors; - fprintf(stderr, "Warning: setting " - "sector offset for DOS " - "compatiblity\n"); - } - update_units(); - break; - case 'v': verify(); - break; - case 'w': write_table(); - default: xmenu(); - } - } -} - -void try(char *device) -{ - disk_device = device; - if (!setjmp(listingbuf)) { - if ((fd = open(disk_device, type_open)) >= 0) { - close(fd); - get_boot(); - list_table(); - if (partitions > 4) - delete_partition(ext_index); - } else { - /* Ignore other errors, since we try IDE - and SCSI hard disks which may not be - installed on the system. */ - if(errno == EACCES) { - fprintf(stderr, "Cannot open %s\n", device); - exit(1); - } - } - } -} - -void main(int argc, char **argv) -{ - if (argc > 3) - fatal(usage); - if (argc > 1 && *argv[1] == '-') { - switch (*(argv[1] + 1)) { - case 'v': - printf("fdisk v" VERSION "\n"); - exit(0); - case 'l': - listing = 1; - type_open = O_RDONLY; - try("/dev/hda"); - try("/dev/hdb"); - try("/dev/hdc"); - try("/dev/hdd"); - try("/dev/sda"); - try("/dev/sdb"); - try("/dev/sdc"); - try("/dev/sdd"); - try("/dev/sde"); - try("/dev/sdf"); - try("/dev/sdg"); - try("/dev/sdh"); - exit(0); - case 's': { - int i; - if (argc < 3) - fatal(usage); - if (!(i = atoi(argv[2] + 8))) - fatal(usage); - disk_device = (char *) malloc(9); - strncpy(disk_device, argv[2], 8); - if ((fd = open(disk_device, O_RDWR)) >= 0) { - close(fd); - get_boot(); - if (i > partitions) exit(1); - if (part_table[--i]->sys_ind > 10) - printf("%d\n", - SWAP32(part_table[i]->nr_sects) / 2); - else exit(1); - exit(0); - } - } - default: - fatal(usage); - } - } - if (argc > 1) - disk_device = argv[argc - 1]; - else if ((fd = open(DEFAULT_DEVICE, O_RDWR)) < 0) - disk_device = ALTERNATE_DEVICE; - else close(fd); - - if (argc == 1) - printf("Using %s as default device!\n", disk_device); - get_boot(); - - while (1) { - putchar('\n'); - switch (tolower(read_char("Command (m for help): "))) { - case 'a': toggle_active(get_partition(1, partitions)); - break; -/* There should be a define which allows to turn off disklabel support so as - not to make fdisk larger than necessary on installation boot disks. -*/ -#if 1 /* #ifndef NO_DISKLABEL_SUPPORT */ - case 'b': bselect(); /* bf */ - break; -#endif - case 'c': - toggle_dos(); - break; - case 'd': delete_partition( - get_partition(1, partitions)); - break; - case 'l': list_types(sys_types, nsys_types); /* bf */ - break; - case 'n': new_partition(); - break; - case 'p': list_table(); - break; - case 'q': close(fd); - exit(0); - case 't': change_sysid(); - break; - case 'u': change_units(); - break; - case 'v': verify(); - break; - case 'w': write_table(); - case 'x': xselect(); - break; - default: menu(); - } - } -} diff --git a/fdisk.h b/fdisk.h deleted file mode 100644 index 2b6ddc8..0000000 --- a/fdisk.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - fdisk.h -*/ - -#define SECTOR_SIZE 512 -#define NETBSD_PARTITION 0xa5 -#define cround(n) (((n) + display_factor * unit_flag) / display_factor) - -#if defined(__GNUC__) || defined(HAS_LONG_LONG) -typedef long long ext2_loff_t; -#else -typedef long ext2_loff_t; -#endif - -#if BYTE_ORDER == BIG_ENDIAN -#define SWAP16(x) __swap16(x) -#define SWAP32(x) __swap32(x) -#else -#define SWAP16(x) ((__u16)x) -#define SWAP32(x) ((__u32)x) -#endif - -enum failure {usage, unable_to_open, unable_to_read, unable_to_seek, - unable_to_write, out_of_memory}; - -enum offset {ignore, lower, deflt, upper}; - -struct systypes { - unsigned char index; - char *name; -}; - -/* prototypes for fdisk.c */ -extern char *disk_device, - *line_ptr; -extern int fd, - partitions; -extern uint unit_flag, - display_factor; -extern struct partition *part_table[]; -extern void fatal(enum failure why); -extern int get_partition(int warn, int max); -extern void list_types(struct systypes *sys, int size); -extern int read_line (void); -extern char read_char(char *mesg); -extern int read_hex(struct systypes *sys, int size); -uint read_int(uint low, uint dflt, uint high, enum offset base, char *mesg); -extern char *const str_units(void); - -/* prototypes for fdisklabel.c */ -extern void bselect(void); diff --git a/fdisklabel.c b/fdisklabel.c deleted file mode 100644 index 70c3cfa..0000000 --- a/fdisklabel.c +++ /dev/null @@ -1,800 +0,0 @@ -/* - NetBSD disklabel editor for Linux fdisk - Written by Bernhard Fastenrath (fasten@informatik.uni-bonn.de) - with code from the NetBSD disklabel command: - - Copyright (c) 1987, 1988 Regents of the University of California. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. All advertising materials mentioning features or use of this software - must display the following acknowledgement: - This product includes software developed by the University of - California, Berkeley and its contributors. - 4. Neither the name of the University nor the names of its contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - SUCH DAMAGE. -*/ - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "kernel-defs.h" - -#include "fdisk.h" -#define DKTYPENAMES -#include "fdisklabel.h" - -static void bsd_delete_part (void); -static void bsd_new_part (void); -static void bsd_print_disklabel (int show_all); -static void bsd_write_disklabel (void); -static int bsd_create_disklabel (void); -static void bsd_edit_disklabel (void); -static void bsd_write_bootstrap (void); -static void bsd_change_fstype (void); -static int bsd_get_part_index (int max); -static int bsd_check_new_partition (int *i); -static void bsd_list_types (void); -static u_short bsd_dkcksum (struct disklabel *lp); -static int bsd_initlabel (struct partition *p, struct disklabel *d, int pindex); -static int bsd_readlabel (struct partition *p, struct disklabel *d); -static int bsd_writelabel (struct partition *p, struct disklabel *d); -static void sync_disks (void); -#if defined (i386) -static int bsd_translate_fstype (int linux_type); -static void bsd_link_part (void); -#endif -#if defined (__alpha__) -void alpha_bootblock_checksum (char *boot); -#endif - -static struct disklabel bsd_dlabel; -static char buffer[BSD_BBSIZE]; -#if defined (i386) -static struct partition *bsd_part; -static int bsd_part_index; -#endif - -void -bmenu (void) -{ - puts ("Command action\n" - " d delete a BSD partition\n" - " e edit drive data\n" - " i install bootstrap\n" - " l list known filesystem types\n" - " m print this menu\n" - " n add a new BSD partition\n" - " p print BSD partition table\n" - " q quit without saving changes\n" -#if defined (i386) - " r return to main menu\n" -#endif - " s show complete disklabel\n" - " t change a partition's filesystem id\n" - " w write disklabel to disk\n" -#if defined (i386) - " x link BSD partition to non-BSD partition" -#endif - ); -} - -void -bselect (void) -{ -#if defined (i386) - int t; - - for (t=0; t<4; t++) - if (part_table[t] -> sys_ind == NETBSD_PARTITION) - { - bsd_part = part_table[t]; - bsd_part_index = t; - if (bsd_part -> start_sect == 0) - { - fprintf (stderr, "Partition %s%d has invalid starting sector 0.\n", - disk_device, t+1); - return; - } - printf ("Reading disklabel of %s%d at sector %d.\n", - disk_device, t+1, bsd_part -> start_sect + BSD_LABELSECTOR); - if (bsd_readlabel (bsd_part, &bsd_dlabel) == 0) - if (bsd_create_disklabel () == 0) - return; - break; - } - - if (t == 4) - { - printf ("There is no NetBSD partition on %s.\n", disk_device); - return; - } - -#elif defined (__alpha__) || defined (__powerpc__) - - if (bsd_readlabel (NULL, &bsd_dlabel) == 0) - if (bsd_create_disklabel () == 0) - exit ( EXIT_SUCCESS ); - -#endif - - while (1) - { - putchar ('\n'); - switch (tolower (read_char ("BSD disklabel command (m for help): "))) - { - case 'd': - bsd_delete_part (); - break; - case 'e': - bsd_edit_disklabel (); - break; - case 'i': - bsd_write_bootstrap (); - break; - case 'l': - bsd_list_types (); - break; - case 'n': - bsd_new_part (); - break; - case 'p': - bsd_print_disklabel (0); - break; - case 'q': - close (fd); - exit ( EXIT_SUCCESS ); - case 's': - bsd_print_disklabel (1); - break; - case 't': - bsd_change_fstype (); - break; - case 'w': - bsd_write_disklabel (); - break; -#if defined (i386) - case 'r': - return; - case 'x': - bsd_link_part (); - break; -#endif - default: - bmenu (); - break; - } - } -} - -static void -bsd_delete_part (void) -{ - int i; - - i = bsd_get_part_index (bsd_dlabel.d_npartitions); - bsd_dlabel.d_partitions[i].p_size = 0; - bsd_dlabel.d_partitions[i].p_offset = 0; - bsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED; - if (bsd_dlabel.d_npartitions == i + 1) - while (bsd_dlabel.d_partitions[bsd_dlabel.d_npartitions-1].p_size == 0) - bsd_dlabel.d_npartitions--; -} - -static void -bsd_new_part (void) -{ - uint begin, end; - char mesg[48]; - int i; - - if (!bsd_check_new_partition (&i)) - return; - -#if defined (i386) - begin = bsd_part -> start_sect; - end = begin + bsd_part -> nr_sects - 1; -#elif defined (__alpha__) || defined (__powerpc__) - begin = 0; - end = bsd_dlabel.d_secperunit; -#endif - - sprintf (mesg, "First %s", str_units()); - begin = read_int (cround (begin), cround (begin), cround (end), ignore, mesg); - - sprintf (mesg, "Last %s or +size or +sizeM or +sizeK", str_units()); - end = read_int (cround (begin), cround (end), cround (end), ignore, mesg); - - if (unit_flag) - { - begin = (begin - 1) * display_factor; - end = end * display_factor - 1; - } - bsd_dlabel.d_partitions[i].p_size = end - begin + 1; - bsd_dlabel.d_partitions[i].p_offset = begin; - bsd_dlabel.d_partitions[i].p_fstype = BSD_FS_UNUSED; -} - -static void -bsd_print_disklabel (int show_all) -{ - struct disklabel *lp = &bsd_dlabel; - struct bsd_partition *pp; - FILE *f = stdout; - int i, j; - - if (show_all) - { -#if defined (i386) - fprintf(f, "# %s%d:\n", disk_device, bsd_part_index+1); -#elif defined (__alpha__) || defined (__powerpc__) - fprintf(f, "# %s:\n", disk_device); -#endif - if ((unsigned) lp->d_type < BSD_DKMAXTYPES) - fprintf(f, "type: %s\n", bsd_dktypenames[lp->d_type]); - else - fprintf(f, "type: %d\n", lp->d_type); - fprintf(f, "disk: %.*s\n", sizeof(lp->d_typename), lp->d_typename); - fprintf(f, "label: %.*s\n", sizeof(lp->d_packname), lp->d_packname); - fprintf(f, "flags:"); - if (lp->d_flags & BSD_D_REMOVABLE) - fprintf(f, " removable"); - if (lp->d_flags & BSD_D_ECC) - fprintf(f, " ecc"); - if (lp->d_flags & BSD_D_BADSECT) - fprintf(f, " badsect"); - fprintf(f, "\n"); - fprintf(f, "bytes/sector: %d\n", lp->d_secsize); - fprintf(f, "sectors/track: %d\n", lp->d_nsectors); - fprintf(f, "tracks/cylinder: %d\n", lp->d_ntracks); - fprintf(f, "sectors/cylinder: %d\n", lp->d_secpercyl); - fprintf(f, "cylinders: %d\n", lp->d_ncylinders); - fprintf(f, "rpm: %d\n", lp->d_rpm); - fprintf(f, "interleave: %d\n", lp->d_interleave); - fprintf(f, "trackskew: %d\n", lp->d_trackskew); - fprintf(f, "cylinderskew: %d\n", lp->d_cylskew); - fprintf(f, "headswitch: %d\t\t# milliseconds\n", lp->d_headswitch); - fprintf(f, "track-to-track seek: %d\t# milliseconds\n", lp->d_trkseek); - fprintf(f, "drivedata: "); - for (i = NDDATA - 1; i >= 0; i--) - if (lp->d_drivedata[i]) - break; - if (i < 0) - i = 0; - for (j = 0; j <= i; j++) - fprintf(f, "%d ", lp->d_drivedata[j]); - } - fprintf (f, "\n%d partitions:\n", lp->d_npartitions); - fprintf (f, "# size offset fstype [fsize bsize cpg]\n"); - pp = lp->d_partitions; - for (i = 0; i < lp->d_npartitions; i++, pp++) { - if (pp->p_size) { - fprintf(f, " %c: %8d %8d ", 'a' + i, - pp->p_size, pp->p_offset); - if ((unsigned) pp->p_fstype < BSD_FSMAXTYPES) - fprintf(f, "%8.8s", bsd_fstypes[pp->p_fstype].name); - else - fprintf(f, "%8x", pp->p_fstype); - switch (pp->p_fstype) - { - case BSD_FS_UNUSED: - fprintf(f, " %5d %5d %5.5s ", - pp->p_fsize, pp->p_fsize * pp->p_frag, ""); - break; - - case BSD_FS_BSDFFS: - fprintf(f, " %5d %5d %5d ", - pp->p_fsize, pp->p_fsize * pp->p_frag, - pp->p_cpg); - break; - - default: - fprintf(f, "%20.20s", ""); - break; - } - fprintf(f, "\t# (Cyl. %4d", -#if 0 - pp->p_offset / lp->d_secpercyl); /* differs from Linux fdisk */ -#else - pp->p_offset / lp->d_secpercyl + 1); -#endif - if (pp->p_offset % lp->d_secpercyl) - putc('*', f); - else - putc(' ', f); - fprintf(f, "- %d", - (pp->p_offset + - pp->p_size + lp->d_secpercyl - 1) / -#if 0 - lp->d_secpercyl - 1); /* differs from Linux fdisk */ -#else - lp->d_secpercyl); -#endif - if (pp->p_size % lp->d_secpercyl) - putc('*', f); - fprintf(f, ")\n"); - } - } -} - -static void -bsd_write_disklabel (void) -{ -#if defined (i386) - printf ("Writing disklabel to %s%d.\n", disk_device, bsd_part_index+1); - bsd_writelabel (bsd_part, &bsd_dlabel); -#elif defined (__alpha__) || defined (__powerpc__) - printf ("Writing disklabel to %s.\n", disk_device); - bsd_writelabel (NULL, &bsd_dlabel); -#endif -} - -static int -bsd_create_disklabel (void) -{ - char c; - -#if defined (i386) - fprintf (stderr, "%s%d contains no disklabel.\n", - disk_device, bsd_part_index+1); -#elif defined (__alpha__) || defined (__powerpc__) - fprintf (stderr, "%s contains no disklabel.\n", disk_device); -#endif - - while (1) - if ((c = tolower (read_char ("Do you want to create a disklabel? (y/n) "))) == 'y') - { -#if defined (i386) - if (bsd_initlabel (bsd_part, &bsd_dlabel, bsd_part_index) == 1) -#elif defined (__alpha__) || defined (__powerpc__) || defined (__mc68000__) - if (bsd_initlabel (NULL, &bsd_dlabel, 0) == 1) -#endif - { - bsd_print_disklabel (1); - return 1; - } - else - return 0; - } - else if (c == 'n') - return 0; -} - -static int -edit_int (int def, char *mesg) -{ - do { - fputs (mesg, stdout); - printf (" (%d): ", def); - if (!read_line ()) - return def; - } - while (!isdigit (*line_ptr)); - return atoi (line_ptr); -} - -static void -bsd_edit_disklabel (void) -{ - struct disklabel *d; - - d = &bsd_dlabel; - -#if defined (__alpha__) || defined (__powerpc__) - d -> d_secsize = (u_long) edit_int ((u_long) d -> d_secsize ,"bytes/sector"); - d -> d_nsectors = (u_long) edit_int ((u_long) d -> d_nsectors ,"sectors/track"); - d -> d_ntracks = (u_long) edit_int ((u_long) d -> d_ntracks ,"tracks/cylinder"); - d -> d_ncylinders = (u_long) edit_int ((u_long) d -> d_ncylinders ,"cylinders"); -#endif - - /* d -> d_secpercyl can be != d -> d_nsectors * d -> d_ntracks */ - while (1) - { - d -> d_secpercyl = (u_long) edit_int ((u_long) d -> d_nsectors * d -> d_ntracks, - "sectors/cylinder"); - if (d -> d_secpercyl <= d -> d_nsectors * d -> d_ntracks) - break; - - printf ("Must be <= sectors/track * tracks/cylinder (default).\n"); - } - d -> d_rpm = (u_short) edit_int ((u_short) d -> d_rpm ,"rpm"); - d -> d_interleave = (u_short) edit_int ((u_short) d -> d_interleave,"interleave"); - d -> d_trackskew = (u_short) edit_int ((u_short) d -> d_trackskew ,"trackskew"); - d -> d_cylskew = (u_short) edit_int ((u_short) d -> d_cylskew ,"cylinderskew"); - d -> d_headswitch = (u_long) edit_int ((u_long) d -> d_headswitch ,"headswitch"); - d -> d_trkseek = (u_long) edit_int ((u_long) d -> d_trkseek ,"track-to-track seek"); - - d -> d_secperunit = d -> d_secpercyl * d -> d_ncylinders; -} - -static int -bsd_get_bootstrap (char *path, void *ptr, int size) -{ - int fd; - - if ((fd = open (path, O_RDONLY)) < 0) - { - perror (path); - return 0; - } - if (read (fd, ptr, size) < 0) - { - perror (path); - close (fd); - return 0; - } - printf (" ... %s\n", path); - close (fd); - return 1; -} - -static void -bsd_write_bootstrap (void) -{ - char *bootdir = BSD_LINUX_BOOTDIR; - char path[MAXPATHLEN]; - char *dkbasename; - struct disklabel dl; - char *d, *p, *e; - int sector; - - if (bsd_dlabel.d_type == BSD_DTYPE_SCSI) - dkbasename = "sd"; - else - dkbasename = "wd"; - - printf ("Bootstrap: %sboot -> boot%s (%s): ", dkbasename, dkbasename, dkbasename); - if (read_line ()) - { - line_ptr[strlen (line_ptr)-1] = '\0'; - dkbasename = line_ptr; - } - sprintf (path, "%s/%sboot", bootdir, dkbasename); - if (!bsd_get_bootstrap (path, buffer, (int) bsd_dlabel.d_secsize)) - return; - - /* We need a backup of the disklabel (bsd_dlabel might have changed). */ - d = &buffer[BSD_LABELSECTOR * SECTOR_SIZE]; - bcopy (d, &dl, sizeof (struct disklabel)); - - /* The disklabel will be overwritten by 0's from bootxx anyway */ - bzero (d, sizeof (struct disklabel)); - - sprintf (path, "%s/boot%s", bootdir, dkbasename); - if (!bsd_get_bootstrap (path, &buffer[bsd_dlabel.d_secsize], - (int) bsd_dlabel.d_bbsize - bsd_dlabel.d_secsize)) - return; - - e = d + sizeof (struct disklabel); - for (p=d; p < e; p++) - if (*p) - { - fprintf (stderr, "Bootstrap overlaps with disk label!\n"); - exit ( EXIT_FAILURE ); - } - - bcopy (&dl, d, sizeof (struct disklabel)); - -#if defined (i386) - sector = bsd_part -> start_sect; -#elif defined (__powerpc__) - sector = 0; -#elif defined (__alpha__) - sector = 0; - alpha_bootblock_checksum (buffer); -#endif - - if (lseek64 (fd, sector * SECTOR_SIZE, SEEK_SET) == -1) - fatal (unable_to_seek); - if (BSD_BBSIZE != write (fd, buffer, BSD_BBSIZE)) - fatal (unable_to_write); - -#if defined (i386) - printf ("Bootstrap installed on %s%d.\n", disk_device, bsd_part_index+1); -#elif defined (__alpha__) || defined (__powerpc__) - printf ("Bootstrap installed on %s.\n", disk_device); -#endif - - sync_disks (); -} - -static void -bsd_change_fstype (void) -{ - int i; - - i = bsd_get_part_index (bsd_dlabel.d_npartitions); - bsd_dlabel.d_partitions[i].p_fstype = read_hex (bsd_fstypes, BSD_FSMAXTYPES); -} - -static int -bsd_get_part_index (int max) -{ - char prompt[40]; - char l; - - sprintf (prompt, "Partition (a-%c): ", 'a' + max - 1); - do - l = tolower (read_char (prompt)); - while (l < 'a' || l > 'a' + max - 1); - return l - 'a'; -} - -static int -bsd_check_new_partition (int *i) -{ - int t; - - if (bsd_dlabel.d_npartitions == BSD_MAXPARTITIONS) - { - for (t=0; t < BSD_MAXPARTITIONS; t++) - if (bsd_dlabel.d_partitions[t].p_size == 0) - break; - - if (t == BSD_MAXPARTITIONS) - { - fprintf (stderr, "The maximum number of partitions has been created\n"); - return 0; - } - } - *i = bsd_get_part_index (BSD_MAXPARTITIONS); - - if (*i >= bsd_dlabel.d_npartitions) - bsd_dlabel.d_npartitions = (*i) + 1; - - if (bsd_dlabel.d_partitions[*i].p_size != 0) - { - fprintf (stderr, "This partition already exists.\n"); - return 0; - } - return 1; -} - -static void -bsd_list_types (void) -{ - list_types (bsd_fstypes, BSD_FSMAXTYPES); -} - -static u_short -bsd_dkcksum (struct disklabel *lp) -{ - register u_short *start, *end; - register u_short sum = 0; - - start = (u_short *)lp; - end = (u_short *)&lp->d_partitions[lp->d_npartitions]; - while (start < end) - sum ^= *start++; - return (sum); -} - -static int -bsd_initlabel (struct partition *p, struct disklabel *d, int pindex) -{ - struct hd_geometry geometry; - struct bsd_partition *pp; - - if (ioctl (fd, HDIO_GETGEO, &geometry) == -1) - { - perror ("ioctl"); - return 0; - } - bzero (d, sizeof (struct disklabel)); - - d -> d_magic = BSD_DISKMAGIC; - - if (strncmp (disk_device, "/dev/sd", 7) == 0) - d -> d_type = BSD_DTYPE_SCSI; - else - d -> d_type = BSD_DTYPE_ST506; - -#if 0 /* not used (at least not written to disk) by NetBSD/i386 1.0 */ - d -> d_subtype = BSD_DSTYPE_INDOSPART & pindex; -#endif - -#if defined (i386) - d -> d_flags = BSD_D_DOSPART; -#else - d -> d_flags = 0; -#endif - d -> d_secsize = SECTOR_SIZE; /* bytes/sector */ - d -> d_nsectors = geometry.sectors; /* sectors/track */ - d -> d_ntracks = geometry.heads; /* tracks/cylinder (heads) */ - d -> d_ncylinders = geometry.cylinders; - d -> d_secpercyl = geometry.sectors * geometry.heads; /* sectors/cylinder */ - d -> d_secperunit = d -> d_secpercyl * d -> d_ncylinders; - - d -> d_rpm = 3600; - d -> d_interleave = 1; - d -> d_trackskew = 0; - d -> d_cylskew = 0; - d -> d_headswitch = 0; - d -> d_trkseek = 0; - - d -> d_magic2 = BSD_DISKMAGIC; - d -> d_bbsize = BSD_BBSIZE; - d -> d_sbsize = BSD_SBSIZE; - -#if defined (i386) - d -> d_npartitions = 4; - pp = &d -> d_partitions[2]; /* Partition C should be the NetBSD partition */ - pp -> p_offset = p -> start_sect; - pp -> p_size = p -> nr_sects; - pp -> p_fstype = BSD_FS_UNUSED; - pp = &d -> d_partitions[3]; /* Partition D should be the whole disk */ - pp -> p_offset = 0; - pp -> p_size = d -> d_secperunit; - pp -> p_fstype = BSD_FS_UNUSED; -#elif defined (__alpha__) || defined (__powerpc__) - d -> d_npartitions = 3; - pp = &d -> d_partitions[2]; /* Partition C should be the whole disk */ - pp -> p_offset = 0; - pp -> p_size = d -> d_secperunit; - pp -> p_fstype = BSD_FS_UNUSED; -#endif - - return 1; -} - -static int -bsd_readlabel (struct partition *p, struct disklabel *d) -{ - int t, sector; - -#if defined (i386) - sector = p -> start_sect; -#elif defined (__alpha__) || defined (__powerpc__) - sector = 0; -#endif - - if (lseek64 (fd, sector * SECTOR_SIZE, SEEK_SET) == -1) - fatal (unable_to_seek); - if (BSD_BBSIZE != read (fd, buffer, BSD_BBSIZE)) - fatal (unable_to_read); - - bcopy (&buffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET], - d, sizeof (struct disklabel)); - - for (t = d -> d_npartitions; t < BSD_MAXPARTITIONS; t++) - { - d -> d_partitions[t].p_size = 0; - d -> d_partitions[t].p_offset = 0; - d -> d_partitions[t].p_fstype = BSD_FS_UNUSED; - } - if (d -> d_magic != BSD_DISKMAGIC || d -> d_magic2 != BSD_DISKMAGIC) - return 0; - - if (d -> d_npartitions > BSD_MAXPARTITIONS) - fprintf (stderr, "Warning: too many partitions (%d, maximum is %d).\n", - d -> d_npartitions, BSD_MAXPARTITIONS); - return 1; -} - -static int -bsd_writelabel (struct partition *p, struct disklabel *d) -{ - int sector; - -#if defined (i386) - sector = p -> start_sect + BSD_LABELSECTOR; -#elif defined (__alpha__) || defined (__powerpc__) - sector = BSD_LABELSECTOR; -#endif - - d -> d_checksum = 0; - d -> d_checksum = bsd_dkcksum (d); - - /* This is necessary if we want to write the bootstrap later, - otherwise we'd write the old disklabel with the bootstrap. - */ - bcopy (d, &buffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET], - sizeof (struct disklabel)); - -#if defined (__alpha__) && BSD_LABELSECTOR == 0 - alpha_bootblock_checksum (buffer); - if (lseek64 (fd, 0, SEEK_SET) == -1) - fatal (unable_to_seek); - if (BSD_BBSIZE != write (fd, buffer, BSD_BBSIZE)) - fatal (unable_to_write); -#else - if (lseek64 (fd, sector * SECTOR_SIZE + BSD_LABELOFFSET, SEEK_SET) == -1) - fatal (unable_to_seek); - if (sizeof (struct disklabel) != write (fd, d, sizeof (struct disklabel))) - fatal (unable_to_write); -#endif - - sync_disks (); - - return 1; -} - -static void -sync_disks (void) -{ - printf ("\nSyncing disks.\n"); - sync (); - sleep (4); -} - -#if defined (i386) -static int -bsd_translate_fstype (int linux_type) -{ - switch (linux_type) - { - case 0x01: /* DOS 12-bit FAT */ - case 0x04: /* DOS 16-bit <32M */ - case 0x06: /* DOS 16-bit >=32M */ - case 0xe1: /* DOS access */ - case 0xe3: /* DOS R/O */ - case 0xf2: /* DOS secondary */ - return BSD_FS_MSDOS; - case 0x07: /* OS/2 HPFS */ - return BSD_FS_HPFS; - default: - return BSD_FS_OTHER; - } -} - -static void -bsd_link_part (void) -{ - int k, i; - - k = get_partition (1, partitions); - - if (!bsd_check_new_partition (&i)) - return; - - bsd_dlabel.d_partitions[i].p_size = part_table[k] -> nr_sects; - bsd_dlabel.d_partitions[i].p_offset = part_table[k] -> start_sect; - bsd_dlabel.d_partitions[i].p_fstype = - bsd_translate_fstype (part_table[k] -> sys_ind); -} -#endif - -#if defined (__alpha__) -typedef unsigned long long u_int64_t; - -void -alpha_bootblock_checksum (char *boot) -{ - u_int64_t *dp, sum; - int i; - - dp = (u_int64_t *)boot; - sum = 0; - for (i = 0; i < 63; i++) - sum += dp[i]; - dp[63] = sum; -} -#endif /* __alpha__ */ diff --git a/fdisklabel.h b/fdisklabel.h deleted file mode 100644 index a9c4d2e..0000000 --- a/fdisklabel.h +++ /dev/null @@ -1,245 +0,0 @@ -/* - * Copyright (c) 1987, 1988 Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#define BSD_DISKMAGIC ((u_long) 0x82564557) -#define BSD_MAXPARTITIONS 8 -#define BSD_LINUX_BOOTDIR "/usr/ucb/mdec" - -#if defined (i386) -#define BSD_LABELSECTOR 1 -#define BSD_LABELOFFSET 0 -#define BSD_BBSIZE 8192 /* size of boot area, with label */ -#define BSD_SBSIZE 8192 /* max size of fs superblock */ -#elif defined (__alpha__) -#error LABELSECTOR, LABELOFFSET, BBSIZE & SBSIZE are undefined for __alpha__ -#define BSD_LABELSECTOR 0 -#define BSD_LABELOFFSET 0 -#define BSD_BBSIZE 0 -#define BSD_SBSIZE 0 -#elif defined (__powerpc__) -/* LABELSECTOR, LABELOFFSET, BBSIZE & SBSIZE are undefined for __powerpc__ */ -#define BSD_LABELSECTOR 0 -#define BSD_LABELOFFSET 0 -#define BSD_BBSIZE 0 -#define BSD_SBSIZE 0 -#elif defined (__mc68000__) -/* LABELSECTOR, LABELOFFSET, BBSIZE & SBSIZE are undefined for __powerpc__ */ -#define BSD_LABELSECTOR 0 -#define BSD_LABELOFFSET 0 -#define BSD_BBSIZE 0 -#define BSD_SBSIZE 0 -#else -#error unknown architecture -#endif - -struct disklabel { - u_long d_magic; /* the magic number */ - short d_type; /* drive type */ - short d_subtype; /* controller/d_type specific */ - char d_typename[16]; /* type name, e.g. "eagle" */ - /* - * d_packname contains the pack identifier and is returned when - * the disklabel is read off the disk or in-core copy. - * d_boot0 and d_boot1 are the (optional) names of the - * primary (block 0) and secondary (block 1-15) bootstraps - * as found in /usr/mdec. These are returned when using - * getdiskbyname(3) to retrieve the values from /etc/disktab. - */ -#if defined(KERNEL) || defined(STANDALONE) - char d_packname[16]; /* pack identifier */ -#else - union { - char un_d_packname[16]; /* pack identifier */ - struct { - char *un_d_boot0; /* primary bootstrap name */ - char *un_d_boot1; /* secondary bootstrap name */ - } un_b; - } d_un; -#define d_packname d_un.un_d_packname -#define d_boot0 d_un.un_b.un_d_boot0 -#define d_boot1 d_un.un_b.un_d_boot1 -#endif /* ! KERNEL or STANDALONE */ - /* disk geometry: */ - u_long d_secsize; /* # of bytes per sector */ - u_long d_nsectors; /* # of data sectors per track */ - u_long d_ntracks; /* # of tracks per cylinder */ - u_long d_ncylinders; /* # of data cylinders per unit */ - u_long d_secpercyl; /* # of data sectors per cylinder */ - u_long d_secperunit; /* # of data sectors per unit */ - /* - * Spares (bad sector replacements) below - * are not counted in d_nsectors or d_secpercyl. - * Spare sectors are assumed to be physical sectors - * which occupy space at the end of each track and/or cylinder. - */ - u_short d_sparespertrack; /* # of spare sectors per track */ - u_short d_sparespercyl; /* # of spare sectors per cylinder */ - /* - * Alternate cylinders include maintenance, replacement, - * configuration description areas, etc. - */ - u_long d_acylinders; /* # of alt. cylinders per unit */ - - /* hardware characteristics: */ - /* - * d_interleave, d_trackskew and d_cylskew describe perturbations - * in the media format used to compensate for a slow controller. - * Interleave is physical sector interleave, set up by the formatter - * or controller when formatting. When interleaving is in use, - * logically adjacent sectors are not physically contiguous, - * but instead are separated by some number of sectors. - * It is specified as the ratio of physical sectors traversed - * per logical sector. Thus an interleave of 1:1 implies contiguous - * layout, while 2:1 implies that logical sector 0 is separated - * by one sector from logical sector 1. - * d_trackskew is the offset of sector 0 on track N - * relative to sector 0 on track N-1 on the same cylinder. - * Finally, d_cylskew is the offset of sector 0 on cylinder N - * relative to sector 0 on cylinder N-1. - */ - u_short d_rpm; /* rotational speed */ - u_short d_interleave; /* hardware sector interleave */ - u_short d_trackskew; /* sector 0 skew, per track */ - u_short d_cylskew; /* sector 0 skew, per cylinder */ - u_long d_headswitch; /* head switch time, usec */ - u_long d_trkseek; /* track-to-track seek, usec */ - u_long d_flags; /* generic flags */ -#define NDDATA 5 - u_long d_drivedata[NDDATA]; /* drive-type specific information */ -#define NSPARE 5 - u_long d_spare[NSPARE]; /* reserved for future use */ - u_long d_magic2; /* the magic number (again) */ - u_short d_checksum; /* xor of data incl. partitions */ - /* filesystem and partition information: */ - u_short d_npartitions; /* number of partitions in following */ - u_long d_bbsize; /* size of boot area at sn0, bytes */ - u_long d_sbsize; /* max size of fs superblock, bytes */ - struct bsd_partition { /* the partition table */ - u_long p_size; /* number of sectors in partition */ - u_long p_offset; /* starting sector */ - u_long p_fsize; /* filesystem basic fragment size */ - u_char p_fstype; /* filesystem type, see below */ - u_char p_frag; /* filesystem fragments per block */ - u_short p_cpg; /* filesystem cylinders per group */ - } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */ -}; - -/* d_type values: */ -#define BSD_DTYPE_SMD 1 /* SMD, XSMD; VAX hp/up */ -#define BSD_DTYPE_MSCP 2 /* MSCP */ -#define BSD_DTYPE_DEC 3 /* other DEC (rk, rl) */ -#define BSD_DTYPE_SCSI 4 /* SCSI */ -#define BSD_DTYPE_ESDI 5 /* ESDI interface */ -#define BSD_DTYPE_ST506 6 /* ST506 etc. */ -#define BSD_DTYPE_HPIB 7 /* CS/80 on HP-IB */ -#define BSD_DTYPE_HPFL 8 /* HP Fiber-link */ -#define BSD_DTYPE_FLOPPY 10 /* floppy */ - -/* d_subtype values: */ -#define BSD_DSTYPE_INDOSPART 0x8 /* is inside dos partition */ -#define BSD_DSTYPE_DOSPART(s) ((s) & 3) /* dos partition number */ -#define BSD_DSTYPE_GEOMETRY 0x10 /* drive params in label */ - -#ifdef DKTYPENAMES -static char *bsd_dktypenames[] = { - "unknown", - "SMD", - "MSCP", - "old DEC", - "SCSI", - "ESDI", - "ST506", - "HP-IB", - "HP-FL", - "type 9", - "floppy", - 0 -}; -#define BSD_DKMAXTYPES (sizeof(bsd_dktypenames) / sizeof(bsd_dktypenames[0]) - 1) -#endif - -/* - * Filesystem type and version. - * Used to interpret other filesystem-specific - * per-partition information. - */ -#define BSD_FS_UNUSED 0 /* unused */ -#define BSD_FS_SWAP 1 /* swap */ -#define BSD_FS_V6 2 /* Sixth Edition */ -#define BSD_FS_V7 3 /* Seventh Edition */ -#define BSD_FS_SYSV 4 /* System V */ -#define BSD_FS_V71K 5 /* V7 with 1K blocks (4.1, 2.9) */ -#define BSD_FS_V8 6 /* Eighth Edition, 4K blocks */ -#define BSD_FS_BSDFFS 7 /* 4.2BSD fast file system */ -#define BSD_FS_MSDOS 8 /* MS-DOS file system */ -#define BSD_FS_BSDLFS 9 /* 4.4BSD log-structured file system */ -#define BSD_FS_OTHER 10 /* in use, but unknown/unsupported */ -#define BSD_FS_HPFS 11 /* OS/2 high-performance file system */ -#define BSD_FS_ISO9660 12 /* ISO-9660 filesystem (cdrom) */ -#define BSD_FS_ISOFS BSD_FS_ISO9660 -#define BSD_FS_BOOT 13 /* partition contains bootstrap */ -#define BSD_FS_ADOS 14 /* AmigaDOS fast file system */ -#define BSD_FS_HFS 15 /* Macintosh HFS */ - -#ifdef DKTYPENAMES -static struct systypes bsd_fstypes[] = { - {BSD_FS_UNUSED, "unused"}, - {BSD_FS_SWAP, "swap"}, - {BSD_FS_V6, "Version 6"}, - {BSD_FS_V7, "Version 7"}, - {BSD_FS_SYSV, "System V"}, - {BSD_FS_V71K, "4.1BSD"}, - {BSD_FS_V8, "Eighth Edition"}, - {BSD_FS_BSDFFS, "4.2BSD"}, - {BSD_FS_MSDOS, "MS-DOS"}, - {BSD_FS_BSDLFS, "4.4LFS"}, - {BSD_FS_OTHER, "unknown"}, - {BSD_FS_HPFS, "HPFS"}, - {BSD_FS_ISO9660,"ISO-9660"}, - {BSD_FS_BOOT, "boot"}, - {BSD_FS_ADOS, "ADOS"}, - {BSD_FS_HFS, "HFS"} -}; - -#define BSD_FSMAXTYPES (sizeof(bsd_fstypes) / sizeof(struct systypes)) -#endif - -/* - * flags shared by various drives: - */ -#define BSD_D_REMOVABLE 0x01 /* removable media */ -#define BSD_D_ECC 0x02 /* supports ECC */ -#define BSD_D_BADSECT 0x04 /* supports bad sector forw. */ -#define BSD_D_RAMDISK 0x08 /* disk emulator */ -#define BSD_D_CHAIN 0x10 /* can do back-back transfers */ -#define BSD_D_DOSPART 0x20 /* within MSDOS partition */ diff --git a/io.c b/io.c index 0937e29..01158a9 100644 --- a/io.c +++ b/io.c @@ -26,9 +26,9 @@ */ #include -#ifndef __linux__ #include #include +#ifndef __linux__ #include #else #ifdef __GLIBC__ diff --git a/partition_map.c b/partition_map.c index 6ca2c16..5ad3de7 100644 --- a/partition_map.c +++ b/partition_map.c @@ -26,18 +26,15 @@ */ #include -#ifndef __linux__ #include #include -#endif #include +#include #include -#ifdef __linux__ #include -#include "kernel-defs.h" #include -#endif +#include // For IOCTLs #include "partition_map.h" #include "pdisk.h" @@ -87,7 +84,7 @@ void coerce_block0(partition_map_header *map); int contains_driver(partition_map *entry); void combine_entry(partition_map *entry); long compute_device_size(int fd); -DPME* create_data(const char *name, const char *dptype, u32 base, u32 length); +DPME* create_data(const char *name, const char *dptype, uint32_t base, uint32_t length); partition_map_header* create_partition_map(char *name); void delete_entry(partition_map *entry); void insert_in_base_order(partition_map *entry); @@ -197,7 +194,7 @@ int read_partition_map(partition_map_header *map) { DPME *data; - u32 limit; + uint32_t limit; int index; data = (DPME *) malloc(PBLOCK_SIZE); @@ -515,17 +512,17 @@ coerce_block0(partition_map_header *map) int -add_partition_to_map(const char *name, const char *dptype, u32 base, u32 length, +add_partition_to_map(const char *name, const char *dptype, uint32_t base, uint32_t length, partition_map_header *map) { partition_map * cur; DPME *data; enum add_action act; int limit; - u32 adjusted_base; - u32 adjusted_length; - u32 new_base; - u32 new_length; + uint32_t adjusted_base; + uint32_t adjusted_length; + uint32_t new_base; + uint32_t new_length; // find a block that starts includes base and length cur = map->base_order; @@ -615,7 +612,7 @@ add_partition_to_map(const char *name, const char *dptype, u32 base, u32 length, DPME * -create_data(const char *name, const char *dptype, u32 base, u32 length) +create_data(const char *name, const char *dptype, uint32_t base, uint32_t length) { DPME *data; diff --git a/partition_map.h b/partition_map.h index 526f605..abf6d13 100644 --- a/partition_map.h +++ b/partition_map.h @@ -24,18 +24,11 @@ * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ - +#ifndef partition_map_h +#define partition_map_h #include "dpme.h" +#include - -// -// Defines -// - - -// -// Types -// struct partition_map_header { int fd; char *name; @@ -47,7 +40,7 @@ struct partition_map_header { int regular_file; int blocks_in_map; int maximum_in_map; - unsigned long media_size; + uint32_t media_size; }; typedef struct partition_map_header partition_map_header; @@ -75,15 +68,10 @@ extern const char * kBootstrapName; extern const char * kFreeName; -// -// Global Variables -// - - // // Forward declarations // -int add_partition_to_map(const char *name, const char *dptype, u32 base, u32 length, partition_map_header *map); +int add_partition_to_map(const char *name, const char *dptype, uint32_t base, uint32_t length, partition_map_header *map); void close_partition_map(partition_map_header *map); void delete_partition_from_map(partition_map *entry); partition_map* find_entry_by_disk_address(long index, partition_map_header *map); @@ -92,3 +80,5 @@ void move_entry_in_map(long old_index, long index, partition_map_header *map); partition_map_header* open_partition_map(char *name, int *valid_file); void resize_map(long new_size, partition_map_header *map); void write_partition_map(partition_map_header *map); + +#endif diff --git a/pdisk.c b/pdisk.c index a9bc629..4098bf4 100644 --- a/pdisk.c +++ b/pdisk.c @@ -28,20 +28,13 @@ */ #include -#ifdef __linux__ #include #include -#else -#include -#include -#include -#endif #include +#include +#include -#ifdef __linux__ #include -#include "kernel-defs.h" -#endif #include "pdisk.h" #include "io.h" @@ -55,9 +48,6 @@ // Defines // #define ARGV_CHUNK 5 -#ifdef __linux__ -#define std_main main -#endif // @@ -109,7 +99,6 @@ void print_notes(); // // Routines // -#ifdef __linux__ int main(int argc, char **argv) { @@ -155,97 +144,8 @@ main(int argc, char **argv) } exit(err); } -#else -main() -{ - char *name; - int command; - int first = 1; - - printf("This app uses the SIOUX console library\n"); - printf("Choose 'Quit' from the file menu to quit.\n\n"); - printf("Use MkLinux style disk names (i.e. /dev/sda, /dev/sdb, etc.).\n\n"); - - SIOUXSettings.autocloseonquit = 0; /* Do we close the SIOUX window on program termination ... */ - SIOUXSettings.asktosaveonclose = 0; /* Do we offer to save on a close ... */ - - if (sizeof(DPME) != PBLOCK_SIZE) { - fatal(-1, "Size of partion map entry (%d) " - "is not equal to block size (%d)\n", - sizeof(DPME), PBLOCK_SIZE); - } - if (sizeof(Block0) != PBLOCK_SIZE) { - fatal(-1, "Size of block zero structure (%d) " - "is not equal to block size (%d)\n", - sizeof(Block0), PBLOCK_SIZE); - } - init_program_name(NULL); - - while (get_command("Top level command (? for help): ", first, &command)) { - first = 0; - - switch (command) { - case '?': - print_notes(); - case 'H': - case 'h': - printf("Commands are:\n"); - printf(" h print help\n"); - printf(" v print the version number and release date\n"); - printf(" l list device's map\n"); - printf(" L list all devices' maps\n"); - printf(" e edit device's map\n"); - printf(" r toggle readonly flag\n"); - printf(" q quit the program\n"); - break; - case 'Q': - case 'q': - goto finis; - break; - case 'V': - case 'v': - printf("version " VERSION " (" RELEASE_DATE ")\n"); - break; - case 'L': - list_all_disks(); - break; - case 'l': - if (get_string_argument("Name of device: ", &name, 1) == 0) { - bad_input("Bad name"); - break; - } - dump(name); - break; - case 'E': - case 'e': - if (get_string_argument("Name of device: ", &name, 1) == 0) { - bad_input("Bad name"); - break; - } - edit(name); - break; - case 'R': - case 'r': - if (rflag) { - rflag = 0; - } else { - rflag = 1; - } - printf("Now in %s mode.\n", (rflag)?"readonly":"read/write"); - break; - default: - bad_input("No such command (%c)", command); - break; - } - } -finis: - - printf("Processing stopped: Choose 'Quit' from the file menu to quit.\n\n"); -} -#endif -#ifdef __linux__ int get_options(int argc, char **argv) { @@ -312,7 +212,6 @@ get_options(int argc, char **argv) } return optind; } -#endif // // Edit the file @@ -322,11 +221,7 @@ edit(char *name) { partition_map_header *map; int command; -#ifdef __linux__ int first = 1; -#else - int first = 0; -#endif int order; int get_type; int valid_file;