/* cmdGetAutoexecBat - Creates a temp file to replace c:\autoexec.bat * * Entry - Client (DS:DX) pointer to receive file name * * EXIT - This routine will Terminate the vdm if it fails * And will not return * * * The buffer to receive the file name must be at least 64 bytes */ VOID cmdGetAutoexecBat (VOID) { UNICODE_STRING Unicode; OEM_STRING OemString; ANSI_STRING AnsiString; ExpandConfigFiles(FALSE); RtlInitAnsiString(&AnsiString, pchTmpAutoexecFile); if (!NT_SUCCESS(RtlAnsiStringToUnicodeString(&Unicode,&AnsiString,TRUE)) ) goto ErrExit; OemString.Buffer = (char *)GetVDMAddr(getDS(),getDX()); OemString.MaximumLength = 64; if (!NT_SUCCESS(RtlUnicodeStringToOemString(&OemString,&Unicode,FALSE)) ) goto ErrExit; RtlFreeUnicodeString(&Unicode); return; ErrExit: RcErrorDialogBox(ED_INITMEMERR, pchTmpConfigFile, NULL); TerminateVDM(); // skip cleanup since I insist that we exit! }
NTSTATUS NTAPI NtDisplayString(IN PUNICODE_STRING DisplayString) { OEM_STRING OemString; /* Convert the string to OEM and display it */ RtlUnicodeStringToOemString(&OemString, DisplayString, TRUE); InbvDisplayString(OemString.Buffer); RtlFreeOemString(&OemString); /* Return success */ return STATUS_SUCCESS; }
VOID WINAPI InitCommandLines(VOID) { PRTL_USER_PROCESS_PARAMETERS Params; /* get command line */ Params = NtCurrentPeb()->ProcessParameters; RtlNormalizeProcessParams (Params); /* initialize command line buffers */ CommandLineStringW.Length = Params->CommandLine.Length; CommandLineStringW.MaximumLength = CommandLineStringW.Length + sizeof(WCHAR); CommandLineStringW.Buffer = RtlAllocateHeap(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS | HEAP_ZERO_MEMORY, CommandLineStringW.MaximumLength); if (CommandLineStringW.Buffer == NULL) { return; } RtlInitAnsiString(&CommandLineStringA, NULL); /* Copy command line */ RtlCopyUnicodeString(&CommandLineStringW, &(Params->CommandLine)); CommandLineStringW.Buffer[CommandLineStringW.Length / sizeof(WCHAR)] = 0; /* convert unicode string to ansi (or oem) */ if (bIsFileApiAnsi) RtlUnicodeStringToAnsiString(&CommandLineStringA, &CommandLineStringW, TRUE); else RtlUnicodeStringToOemString(&CommandLineStringA, &CommandLineStringW, TRUE); CommandLineStringA.Buffer[CommandLineStringA.Length] = 0; bCommandLineInitialized = TRUE; }
BOOL WINAPI WriteConsoleOutputCharacterW( HANDLE hConsoleOutput, IN LPCWSTR lpCharacter, IN DWORD nLength, IN COORD dwWriteCoord, OUT LPDWORD lpNumberOfCharsWritten) { IO_STATUS_BLOCK IoStatusBlock; PCHAR Buffer; COORD *pCoord; PCHAR pText; NTSTATUS Status; // ULONG i; UNICODE_STRING UnicodeString; OEM_STRING OemString; ULONG OemLength; UnicodeString.Length = nLength * sizeof(WCHAR); UnicodeString.MaximumLength = nLength * sizeof(WCHAR); UnicodeString.Buffer = (LPWSTR)lpCharacter; OemLength = RtlUnicodeStringToOemSize(&UnicodeString); Buffer = (CHAR*)RtlAllocateHeap(ProcessHeap, 0, OemLength + sizeof(COORD)); // nLength + sizeof(COORD)); if (Buffer== NULL) return FALSE; pCoord = (COORD *)Buffer; pText = (PCHAR)(pCoord + 1); *pCoord = dwWriteCoord; OemString.Length = 0; OemString.MaximumLength = OemLength; OemString.Buffer = pText; Status = RtlUnicodeStringToOemString(&OemString, &UnicodeString, FALSE); if (!NT_SUCCESS(Status)) goto done; /* FIXME: use real unicode->oem conversion */ // for (i = 0; i < nLength; i++) // pText[i] = (CHAR)lpCharacter[i]; Status = NtDeviceIoControlFile(hConsoleOutput, NULL, NULL, NULL, &IoStatusBlock, IOCTL_CONSOLE_WRITE_OUTPUT_CHARACTER, NULL, 0, Buffer, nLength + sizeof(COORD)); done: RtlFreeHeap(ProcessHeap, 0, Buffer); if (!NT_SUCCESS(Status)) return FALSE; *lpNumberOfCharsWritten = IoStatusBlock.Information; return TRUE; }
static NTSTATUS FsdSetFsLabelInformation( PDEVICE_OBJECT DeviceObject, PFILE_FS_LABEL_INFORMATION FsLabelInfo) { PDEVICE_EXTENSION DeviceExt; PVOID Context = NULL; ULONG DirIndex = 0; PDIR_ENTRY Entry; PVFATFCB pRootFcb; LARGE_INTEGER FileOffset; BOOLEAN LabelFound = FALSE; DIR_ENTRY VolumeLabelDirEntry; ULONG VolumeLabelDirIndex; ULONG LabelLen; NTSTATUS Status = STATUS_UNSUCCESSFUL; OEM_STRING StringO; UNICODE_STRING StringW; CHAR cString[43]; ULONG SizeDirEntry; ULONG EntriesPerPage; DPRINT("FsdSetFsLabelInformation()\n"); DeviceExt = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension; if (sizeof(DeviceObject->Vpb->VolumeLabel) < FsLabelInfo->VolumeLabelLength) { return STATUS_NAME_TOO_LONG; } if (DeviceExt->Flags & VCB_IS_FATX) { if (FsLabelInfo->VolumeLabelLength / sizeof(WCHAR) > 42) return STATUS_NAME_TOO_LONG; SizeDirEntry = sizeof(FATX_DIR_ENTRY); EntriesPerPage = FATX_ENTRIES_PER_PAGE; } else { if (FsLabelInfo->VolumeLabelLength / sizeof(WCHAR) > 11) return STATUS_NAME_TOO_LONG; SizeDirEntry = sizeof(FAT_DIR_ENTRY); EntriesPerPage = FAT_ENTRIES_PER_PAGE; } /* Create Volume label dir entry */ LabelLen = FsLabelInfo->VolumeLabelLength / sizeof(WCHAR); RtlZeroMemory(&VolumeLabelDirEntry, SizeDirEntry); StringW.Buffer = FsLabelInfo->VolumeLabel; StringW.Length = StringW.MaximumLength = (USHORT)FsLabelInfo->VolumeLabelLength; StringO.Buffer = cString; StringO.Length = 0; StringO.MaximumLength = 42; Status = RtlUnicodeStringToOemString(&StringO, &StringW, FALSE); if (!NT_SUCCESS(Status)) return Status; if (DeviceExt->Flags & VCB_IS_FATX) { RtlCopyMemory(VolumeLabelDirEntry.FatX.Filename, cString, LabelLen); memset(&VolumeLabelDirEntry.FatX.Filename[LabelLen], ' ', 42 - LabelLen); VolumeLabelDirEntry.FatX.Attrib = _A_VOLID; } else { RtlCopyMemory(VolumeLabelDirEntry.Fat.Filename, cString, max(sizeof(VolumeLabelDirEntry.Fat.Filename), LabelLen)); if (LabelLen > sizeof(VolumeLabelDirEntry.Fat.Filename)) { memset(VolumeLabelDirEntry.Fat.Ext, ' ', sizeof(VolumeLabelDirEntry.Fat.Ext)); RtlCopyMemory(VolumeLabelDirEntry.Fat.Ext, cString + sizeof(VolumeLabelDirEntry.Fat.Filename), LabelLen - sizeof(VolumeLabelDirEntry.Fat.Filename)); } else { memset(&VolumeLabelDirEntry.Fat.Filename[LabelLen], ' ', sizeof(VolumeLabelDirEntry.Fat.Filename) - LabelLen); } VolumeLabelDirEntry.Fat.Attrib = _A_VOLID; } pRootFcb = vfatOpenRootFCB(DeviceExt); /* Search existing volume entry on disk */ FileOffset.QuadPart = 0; if (CcPinRead(pRootFcb->FileObject, &FileOffset, SizeDirEntry, TRUE, &Context, (PVOID*)&Entry)) { while (TRUE) { if (ENTRY_VOLUME(DeviceExt, Entry)) { /* Update entry */ LabelFound = TRUE; RtlCopyMemory(Entry, &VolumeLabelDirEntry, SizeDirEntry); CcSetDirtyPinnedData(Context, NULL); Status = STATUS_SUCCESS; break; } if (ENTRY_END(DeviceExt, Entry)) { break; } DirIndex++; Entry = (PDIR_ENTRY)((ULONG_PTR)Entry + SizeDirEntry); if ((DirIndex % EntriesPerPage) == 0) { CcUnpinData(Context); FileOffset.u.LowPart += PAGE_SIZE; if (!CcPinRead(pRootFcb->FileObject, &FileOffset, SizeDirEntry, TRUE, &Context, (PVOID*)&Entry)) { Context = NULL; break; } } } if (Context) { CcUnpinData(Context); } } if (!LabelFound) { /* Add new entry for label */ if (!vfatFindDirSpace(DeviceExt, pRootFcb, 1, &VolumeLabelDirIndex)) Status = STATUS_DISK_FULL; else { FileOffset.u.HighPart = 0; FileOffset.u.LowPart = VolumeLabelDirIndex * SizeDirEntry; if (!CcPinRead(pRootFcb->FileObject, &FileOffset, SizeDirEntry, TRUE, &Context, (PVOID*)&Entry)) { Status = STATUS_UNSUCCESSFUL; } else { RtlCopyMemory(Entry, &VolumeLabelDirEntry, SizeDirEntry); CcSetDirtyPinnedData(Context, NULL); CcUnpinData(Context); Status = STATUS_SUCCESS; } } } vfatReleaseFCB(DeviceExt, pRootFcb); if (!NT_SUCCESS(Status)) { return Status; } /* Update volume label in memory */ DeviceObject->Vpb->VolumeLabelLength = (USHORT)FsLabelInfo->VolumeLabelLength; RtlCopyMemory(DeviceObject->Vpb->VolumeLabel, FsLabelInfo->VolumeLabel, DeviceObject->Vpb->VolumeLabelLength); return Status; }
NTSTATUS Fat32Format(IN HANDLE FileHandle, IN PPARTITION_INFORMATION PartitionInfo, IN PDISK_GEOMETRY DiskGeometry, IN PUNICODE_STRING Label, IN BOOLEAN QuickFormat, IN ULONG ClusterSize, IN OUT PFORMAT_CONTEXT Context) { FAT32_BOOT_SECTOR BootSector; OEM_STRING VolumeLabel; ULONG TmpVal1; ULONG TmpVal2; NTSTATUS Status; /* Calculate cluster size */ if (ClusterSize == 0) { if (PartitionInfo->PartitionLength.QuadPart < 8LL * 1024LL * 1024LL * 1024LL) { /* Partition < 8GB ==> 4KB Cluster */ ClusterSize = 4096; } else if (PartitionInfo->PartitionLength.QuadPart < 16LL * 1024LL * 1024LL * 1024LL) { /* Partition 8GB - 16GB ==> 8KB Cluster */ ClusterSize = 8192; } else if (PartitionInfo->PartitionLength.QuadPart < 32LL * 1024LL * 1024LL * 1024LL) { /* Partition 16GB - 32GB ==> 16KB Cluster */ ClusterSize = 16384; } else { /* Partition >= 32GB ==> 32KB Cluster */ ClusterSize = 32768; } } memset(&BootSector, 0, sizeof(FAT32_BOOT_SECTOR)); memcpy(&BootSector.OEMName[0], "MSWIN4.1", 8); BootSector.BytesPerSector = DiskGeometry->BytesPerSector; BootSector.SectorsPerCluster = ClusterSize / BootSector.BytesPerSector; BootSector.ReservedSectors = 32; BootSector.FATCount = 2; BootSector.RootEntries = 0; BootSector.Sectors = 0; BootSector.Media = 0xf8; BootSector.FATSectors = 0; BootSector.SectorsPerTrack = DiskGeometry->SectorsPerTrack; BootSector.Heads = DiskGeometry->TracksPerCylinder; BootSector.HiddenSectors = PartitionInfo->HiddenSectors; BootSector.SectorsHuge = PartitionInfo->PartitionLength.QuadPart >> GetShiftCount(BootSector.BytesPerSector); /* Use shifting to avoid 64-bit division */ BootSector.FATSectors32 = 0; /* Set later */ BootSector.ExtFlag = 0; /* Mirror all FATs */ BootSector.FSVersion = 0x0000; /* 0:0 */ BootSector.RootCluster = 2; BootSector.FSInfoSector = 1; BootSector.BootBackup = 6; BootSector.Drive = (DiskGeometry->MediaType == FixedMedia) ? 0x80 : 0x00; BootSector.ExtBootSignature = 0x29; BootSector.VolumeID = CalcVolumeSerialNumber (); if ((Label == NULL) || (Label->Buffer == NULL)) { memcpy(&BootSector.VolumeLabel[0], "NO NAME ", 11); } else { RtlUnicodeStringToOemString(&VolumeLabel, Label, TRUE); memset(&BootSector.VolumeLabel[0], ' ', 11); memcpy(&BootSector.VolumeLabel[0], VolumeLabel.Buffer, VolumeLabel.Length < 11 ? VolumeLabel.Length : 11); RtlFreeOemString(&VolumeLabel); } memcpy(&BootSector.SysType[0], "FAT32 ", 8); /* Calculate number of FAT sectors */ /* (BytesPerSector / 4) FAT entries (32bit) fit into one sector */ TmpVal1 = BootSector.SectorsHuge - BootSector.ReservedSectors; TmpVal2 = ((BootSector.BytesPerSector / 4) * BootSector.SectorsPerCluster) + BootSector.FATCount; BootSector.FATSectors32 = (TmpVal1 + (TmpVal2 - 1)) / TmpVal2; DPRINT("FATSectors32 = %lu\n", BootSector.FATSectors32); /* Init context data */ Context->TotalSectorCount = 2 + (BootSector.FATSectors32 * BootSector.FATCount) + BootSector.SectorsPerCluster; if (!QuickFormat) { Context->TotalSectorCount += BootSector.SectorsHuge; Status = Fat32WipeSectors(FileHandle, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat32WipeSectors() failed with status 0x%.08x\n", Status); return Status; } } Status = Fat32WriteBootSector(FileHandle, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat32WriteBootSector() failed with status 0x%.08x\n", Status); return Status; } Status = Fat32WriteFsInfo(FileHandle, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat32WriteFsInfo() failed with status 0x%.08x\n", Status); return Status; } /* Write first FAT copy */ Status = Fat32WriteFAT(FileHandle, 0, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat32WriteFAT() failed with status 0x%.08x\n", Status); return Status; } /* Write second FAT copy */ Status = Fat32WriteFAT(FileHandle, BootSector.FATSectors32, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat32WriteFAT() failed with status 0x%.08x.\n", Status); return Status; } Status = Fat32WriteRootDirectory(FileHandle, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat32WriteRootDirectory() failed with status 0x%.08x\n", Status); } return Status; }
NTSTATUS Fat16Format(IN HANDLE FileHandle, IN PPARTITION_INFORMATION PartitionInfo, IN PDISK_GEOMETRY DiskGeometry, IN PUNICODE_STRING Label, IN BOOLEAN QuickFormat, IN ULONG ClusterSize, IN OUT PFORMAT_CONTEXT Context) { FAT16_BOOT_SECTOR BootSector; OEM_STRING VolumeLabel; ULONG SectorCount; ULONG RootDirSectors; ULONG TmpVal1; ULONG TmpVal2; ULONG TmpVal3; NTSTATUS Status; /* Calculate cluster size */ if (ClusterSize == 0) { if (PartitionInfo->PartitionLength.QuadPart < 16LL * 1024LL * 1024LL) { /* Partition < 16MB ==> 1KB Cluster */ ClusterSize = 1024; } else if (PartitionInfo->PartitionLength.QuadPart < 128LL * 1024LL * 1024LL) { /* Partition < 128MB ==> 2KB Cluster */ ClusterSize = 2048; } else if (PartitionInfo->PartitionLength.QuadPart < 256LL * 1024LL * 1024LL) { /* Partition < 256MB ==> 4KB Cluster */ ClusterSize = 4096; } else { /* Partition >= 256MB (< 512MB) ==> 8KB Cluster */ ClusterSize = 8192; } } SectorCount = PartitionInfo->PartitionLength.QuadPart >> GetShiftCount(DiskGeometry->BytesPerSector); /* Use shifting to avoid 64-bit division */ RtlZeroMemory(&BootSector, sizeof(FAT16_BOOT_SECTOR)); memcpy(&BootSector.OEMName[0], "MSWIN4.1", 8); /* FIXME: Add dummy bootloader for real */ BootSector.Jump[0] = 0xeb; BootSector.Jump[1] = 0x3c; BootSector.Jump[2] = 0x90; BootSector.BytesPerSector = DiskGeometry->BytesPerSector; BootSector.SectorsPerCluster = ClusterSize / BootSector.BytesPerSector; BootSector.ReservedSectors = 1; BootSector.FATCount = 2; BootSector.RootEntries = 512; BootSector.Sectors = (SectorCount < 0x10000) ? (unsigned short)SectorCount : 0; BootSector.Media = 0xf8; BootSector.FATSectors = 0; /* Set later. See below. */ BootSector.SectorsPerTrack = DiskGeometry->SectorsPerTrack; BootSector.Heads = DiskGeometry->TracksPerCylinder; BootSector.HiddenSectors = PartitionInfo->HiddenSectors; BootSector.SectorsHuge = (SectorCount >= 0x10000) ? (unsigned long)SectorCount : 0; BootSector.Drive = (DiskGeometry->MediaType == FixedMedia) ? 0x80 : 0x00; BootSector.ExtBootSignature = 0x29; BootSector.VolumeID = CalcVolumeSerialNumber(); if ((Label == NULL) || (Label->Buffer == NULL)) { memcpy(&BootSector.VolumeLabel[0], "NO NAME ", 11); } else { RtlUnicodeStringToOemString(&VolumeLabel, Label, TRUE); RtlFillMemory(&BootSector.VolumeLabel[0], 11, ' '); memcpy(&BootSector.VolumeLabel[0], VolumeLabel.Buffer, VolumeLabel.Length < 11 ? VolumeLabel.Length : 11); RtlFreeOemString(&VolumeLabel); } memcpy(&BootSector.SysType[0], "FAT16 ", 8); DPRINT("BootSector.SectorsHuge = %lx\n", BootSector.SectorsHuge); RootDirSectors = ((BootSector.RootEntries * 32) + (BootSector.BytesPerSector - 1)) / BootSector.BytesPerSector; /* Calculate number of FAT sectors */ /* (BootSector.BytesPerSector / 2) FAT entries (16bit) fit into one sector */ TmpVal1 = SectorCount - (BootSector.ReservedSectors + RootDirSectors); TmpVal2 = ((BootSector.BytesPerSector / 2) * BootSector.SectorsPerCluster) + BootSector.FATCount; TmpVal3 = (TmpVal1 + (TmpVal2 - 1)) / TmpVal2; BootSector.FATSectors = (unsigned short)(TmpVal3 & 0xffff); DPRINT("BootSector.FATSectors = %hu\n", BootSector.FATSectors); /* Init context data */ Context->TotalSectorCount = 1 + (BootSector.FATSectors * 2) + RootDirSectors; if (!QuickFormat) { Context->TotalSectorCount += SectorCount; Status = FatWipeSectors(FileHandle, SectorCount, (ULONG)BootSector.SectorsPerCluster, (ULONG)BootSector.BytesPerSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("FatWipeSectors() failed with status 0x%.08x\n", Status); return Status; } } Status = Fat16WriteBootSector(FileHandle, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat16WriteBootSector() failed with status 0x%.08x\n", Status); return Status; } /* Write first FAT copy */ Status = Fat16WriteFAT(FileHandle, 0, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat16WriteFAT() failed with status 0x%.08x\n", Status); return Status; } /* Write second FAT copy */ Status = Fat16WriteFAT(FileHandle, (ULONG)BootSector.FATSectors, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat16WriteFAT() failed with status 0x%.08x.\n", Status); return Status; } Status = Fat16WriteRootDirectory(FileHandle, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat16WriteRootDirectory() failed with status 0x%.08x\n", Status); } return Status; }
NOT PAGEABLE -- WalkConnectionTable #endif VOID SrvAllocateEndpoint ( OUT PENDPOINT *Endpoint, IN PUNICODE_STRING NetworkName, IN PUNICODE_STRING TransportName, IN PANSI_STRING TransportAddress, IN PUNICODE_STRING DomainName ) /*++ Routine Description: This function allocates an Endpoint Block from the system nonpaged pool. Arguments: Endpoint - Returns a pointer to the endpoint block, or NULL if no pool was available. NetworkName - Supplies a pointer to the network name (e.g., NET1). TransportName - The fully qualified name of the transport device. For example, "\Device\Nbf". TransportAddress - The fully qualified address (or name ) of the server's endpoint. This name is used exactly as specified. For NETBIOS-compatible networks, the caller must upcase and blank-fill the name. E.g., "\Device\Nbf\NTSERVERbbbbbbbb". DomainName - the domain being serviced by this endpoint Return Value: None. --*/ { CLONG length; PENDPOINT endpoint; NTSTATUS status; PAGED_CODE( ); // // Attempt to allocate from nonpaged pool. // length = sizeof(ENDPOINT) + NetworkName->Length + sizeof(*NetworkName->Buffer) + TransportName->Length + sizeof(*TransportName->Buffer) + TransportAddress->Length + sizeof(*TransportAddress->Buffer) + DomainName->Length + sizeof(*DomainName->Buffer) + RtlUnicodeStringToOemSize( DomainName ) + sizeof(CHAR); endpoint = ALLOCATE_NONPAGED_POOL( length, BlockTypeEndpoint ); *Endpoint = endpoint; if ( endpoint == NULL ) { INTERNAL_ERROR ( ERROR_LEVEL_EXPECTED, "SrvAllocateEndpoint: Unable to allocate %d bytes from nonpaged " "pool.", length, NULL ); return; } IF_DEBUG(HEAP) { SrvPrint1( "SrvAllocateEndpoint: Allocated endpoint at %lx\n", endpoint ); } // // Initialize the endpoint block. Zero it first. // RtlZeroMemory( endpoint, length ); SET_BLOCK_TYPE_STATE_SIZE( endpoint, BlockTypeEndpoint, BlockStateActive, length ); endpoint->BlockHeader.ReferenceCount = 2; // allow for Active status // and caller's pointer // // Allocate connection table. // SrvAllocateTable( &endpoint->ConnectionTable, 6, // !!! TRUE ); if ( endpoint->ConnectionTable.Table == NULL ) { DEALLOCATE_NONPAGED_POOL( endpoint ); *Endpoint = NULL; return; } InitializeListHead( &endpoint->FreeConnectionList ); #if SRVDBG29 UpdateConnectionHistory( "INIT", endpoint, NULL ); #endif // // Copy the network name, transport name, and server address, and domain // name into the block. // endpoint->NetworkName.Length = NetworkName->Length; endpoint->NetworkName.MaximumLength = (SHORT)(NetworkName->Length + sizeof(*NetworkName->Buffer)); endpoint->NetworkName.Buffer = (PWCH)(endpoint + 1); RtlCopyMemory( endpoint->NetworkName.Buffer, NetworkName->Buffer, NetworkName->Length ); endpoint->TransportName.Length = TransportName->Length; endpoint->TransportName.MaximumLength = (SHORT)(TransportName->Length + sizeof(*TransportName->Buffer)); endpoint->TransportName.Buffer = (PWCH)((PCHAR)endpoint->NetworkName.Buffer + endpoint->NetworkName.MaximumLength); RtlCopyMemory( endpoint->TransportName.Buffer, TransportName->Buffer, TransportName->Length ); endpoint->TransportAddress.Length = TransportAddress->Length; endpoint->TransportAddress.MaximumLength = (SHORT)(TransportAddress->Length + 1); endpoint->TransportAddress.Buffer = (PCHAR)endpoint->TransportName.Buffer + endpoint->TransportName.MaximumLength; RtlCopyMemory( endpoint->TransportAddress.Buffer, TransportAddress->Buffer, TransportAddress->Length ); endpoint->DomainName.Length = DomainName->Length; endpoint->DomainName.MaximumLength = (SHORT)(DomainName->Length + sizeof(*DomainName->Buffer)); endpoint->DomainName.Buffer = (PWCH)((PCHAR)endpoint->TransportAddress.Buffer + TransportAddress->MaximumLength); RtlCopyMemory( endpoint->DomainName.Buffer, DomainName->Buffer, DomainName->Length ); endpoint->OemDomainName.Length = (SHORT)RtlUnicodeStringToOemSize( DomainName ); endpoint->OemDomainName.MaximumLength = endpoint->OemDomainName.Length + sizeof( CHAR ); endpoint->OemDomainName.Buffer = (PCHAR)endpoint->DomainName.Buffer + DomainName->MaximumLength; status = RtlUnicodeStringToOemString( &endpoint->OemDomainName, &endpoint->DomainName, FALSE // Do not allocate the OEM string ); ASSERT( NT_SUCCESS(status) ); // // Initialize the network address field. // endpoint->NetworkAddress.Buffer = endpoint->NetworkAddressData; endpoint->NetworkAddress.Length = sizeof( endpoint->NetworkAddressData ) - sizeof(endpoint->NetworkAddressData[0]); endpoint->NetworkAddress.MaximumLength = sizeof( endpoint->NetworkAddressData ); // // Increment the count of endpoints in the server. // ACQUIRE_LOCK( &SrvEndpointLock ); SrvEndpointCount++; RELEASE_LOCK( &SrvEndpointLock ); INITIALIZE_REFERENCE_HISTORY( endpoint ); INCREMENT_DEBUG_STAT( SrvDbgStatistics.EndpointInfo.Allocations ); return; } // SrvAllocateEndpoint
NTSTATUS NtDisplayString( IN PUNICODE_STRING String ) /*++ Routine Description: This service calls the HAL to display a string on the console. The caller must have SeTcbPrivilege to display a message. Arguments: String - A pointer to the string that is to be displayed. Return Value: !NT_SUCCESS - The operation failed or the caller did not have appropriate priviledges. --*/ { KPROCESSOR_MODE PreviousMode; UNICODE_STRING CapturedString; PUCHAR StringBuffer = NULL; PUCHAR AnsiStringBuffer = NULL; STRING AnsiString; // // Check to determine if the caller has the privilege to make this // call. // PreviousMode = KeGetPreviousMode(); if (!SeSinglePrivilegeCheck(SeTcbPrivilege, PreviousMode)) { return STATUS_PRIVILEGE_NOT_HELD; } try { // // If the previous mode was user, then check the input parameters. // if (PreviousMode != KernelMode) { // // Probe and capture the input unicode string descriptor. // CapturedString = ProbeAndReadUnicodeString(String); // // If the captured string descriptor has a length of zero, then // return success. // if ((CapturedString.Buffer == 0) || (CapturedString.MaximumLength == 0)) { return STATUS_SUCCESS; } // // Probe and capture the input string. // // N.B. Note the length is in bytes. // ProbeForRead( CapturedString.Buffer, CapturedString.MaximumLength, sizeof(UCHAR) ); // // Allocate a non-paged string buffer because the buffer passed to // HalDisplay string must be non-paged. // StringBuffer = ExAllocatePoolWithTag(NonPagedPool, CapturedString.MaximumLength, 'grtS'); if ( !StringBuffer ) { return STATUS_NO_MEMORY; } RtlMoveMemory(StringBuffer, CapturedString.Buffer, CapturedString.MaximumLength); CapturedString.Buffer = (PWSTR)StringBuffer; // // Allocate a string buffer for the ansi string. // AnsiStringBuffer = ExAllocatePoolWithTag(NonPagedPool, CapturedString.MaximumLength, 'grtS'); if (AnsiStringBuffer == NULL) { ExFreePool(StringBuffer); return STATUS_NO_MEMORY; } AnsiString.MaximumLength = CapturedString.MaximumLength; AnsiString.Length = 0; AnsiString.Buffer = AnsiStringBuffer; // // Transform the string to ANSI until the HAL handles unicode. // RtlUnicodeStringToOemString( &AnsiString, &CapturedString, FALSE ); } else { // // Allocate a string buffer for the ansi string. // AnsiStringBuffer = ExAllocatePoolWithTag(NonPagedPool, String->MaximumLength, 'grtS'); if (AnsiStringBuffer == NULL) { return STATUS_NO_MEMORY; } AnsiString.MaximumLength = String->MaximumLength; AnsiString.Length = 0; AnsiString.Buffer = AnsiStringBuffer; // // We were in kernel mode; just transform the original string. // RtlUnicodeStringToOemString( &AnsiString, String, FALSE ); } HalDisplayString( AnsiString.Buffer ); // // Free up the memory we used to store the strings. // if (PreviousMode != KernelMode) { ExFreePool(StringBuffer); } ExFreePool(AnsiStringBuffer); } except(EXCEPTION_EXECUTE_HANDLER) { if (StringBuffer != NULL) { ExFreePool(StringBuffer); } return GetExceptionCode(); } return STATUS_SUCCESS; }
NTSTATUS Fat12Format(IN HANDLE FileHandle, IN PPARTITION_INFORMATION PartitionInfo, IN PDISK_GEOMETRY DiskGeometry, IN PUNICODE_STRING Label, IN BOOLEAN QuickFormat, IN ULONG ClusterSize, IN OUT PFORMAT_CONTEXT Context) { FAT16_BOOT_SECTOR BootSector; OEM_STRING VolumeLabel; ULONG SectorCount; ULONG RootDirSectors; ULONG TmpVal1; ULONG TmpVal2; ULONG TmpVal3; NTSTATUS Status; /* Calculate cluster size */ if (ClusterSize == 0) { if (DiskGeometry->MediaType == FixedMedia) { /* 4KB Cluster (Harddisk only) */ ClusterSize = 4096; } else { /* 512 byte cluster (floppy) */ ClusterSize = 512; } } SectorCount = PartitionInfo->PartitionLength.QuadPart >> GetShiftCount(DiskGeometry->BytesPerSector); /* Use shifting to avoid 64-bit division */ DPRINT("SectorCount = %lu\n", SectorCount); memset(&BootSector, 0, sizeof(FAT16_BOOT_SECTOR)); memcpy(&BootSector.OEMName[0], "MSWIN4.1", 8); BootSector.BytesPerSector = DiskGeometry->BytesPerSector; BootSector.SectorsPerCluster = ClusterSize / BootSector.BytesPerSector; BootSector.ReservedSectors = 1; BootSector.FATCount = 2; BootSector.RootEntries = 512; BootSector.Sectors = (SectorCount < 0x10000) ? (unsigned short)SectorCount : 0; BootSector.Media = 0xf8; BootSector.FATSectors = 0; /* Set later. See below. */ BootSector.SectorsPerTrack = DiskGeometry->SectorsPerTrack; BootSector.Heads = DiskGeometry->TracksPerCylinder; BootSector.HiddenSectors = PartitionInfo->HiddenSectors; BootSector.SectorsHuge = (SectorCount >= 0x10000) ? (unsigned long)SectorCount : 0; BootSector.Drive = DiskGeometry->MediaType == FixedMedia ? 0x80 : 0x00; BootSector.ExtBootSignature = 0x29; BootSector.VolumeID = CalcVolumeSerialNumber(); if ((Label == NULL) || (Label->Buffer == NULL)) { memcpy(&BootSector.VolumeLabel[0], "NO NAME ", 11); } else { RtlUnicodeStringToOemString(&VolumeLabel, Label, TRUE); memset(&BootSector.VolumeLabel[0], ' ', 11); memcpy(&BootSector.VolumeLabel[0], VolumeLabel.Buffer, VolumeLabel.Length < 11 ? VolumeLabel.Length : 11); RtlFreeOemString(&VolumeLabel); } memcpy(&BootSector.SysType[0], "FAT12 ", 8); RootDirSectors = ((BootSector.RootEntries * 32) + (BootSector.BytesPerSector - 1)) / BootSector.BytesPerSector; /* Calculate number of FAT sectors */ /* ((BootSector.BytesPerSector * 2) / 3) FAT entries (12bit) fit into one sector */ TmpVal1 = SectorCount - (BootSector.ReservedSectors + RootDirSectors); TmpVal2 = (((BootSector.BytesPerSector * 2) / 3) * BootSector.SectorsPerCluster) + BootSector.FATCount; TmpVal3 = (TmpVal1 + (TmpVal2 - 1)) / TmpVal2; BootSector.FATSectors = (unsigned short)(TmpVal3 & 0xffff); DPRINT("BootSector.FATSectors = %hx\n", BootSector.FATSectors); /* Init context data */ Context->TotalSectorCount = 1 + (BootSector.FATSectors * 2) + RootDirSectors; Status = Fat12WriteBootSector(FileHandle, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat12WriteBootSector() failed with status 0x%.08x\n", Status); return Status; } /* Write first FAT copy */ Status = Fat12WriteFAT(FileHandle, 0, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat12WriteFAT() failed with status 0x%.08x\n", Status); return Status; } /* Write second FAT copy */ Status = Fat12WriteFAT(FileHandle, (ULONG)BootSector.FATSectors, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat12WriteFAT() failed with status 0x%.08x.\n", Status); return Status; } Status = Fat12WriteRootDirectory(FileHandle, &BootSector, Context); if (!NT_SUCCESS(Status)) { DPRINT("Fat12WriteRootDirectory() failed with status 0x%.08x\n", Status); } if (!QuickFormat) { /* FIXME: Fill remaining sectors */ } return Status; }
/* * rename an existing FAT entry */ NTSTATUS vfatRenameEntry( IN PDEVICE_EXTENSION DeviceExt, IN PVFATFCB pFcb, IN PUNICODE_STRING FileName, IN BOOLEAN CaseChangeOnly) { OEM_STRING NameA; ULONG StartIndex; PVOID Context = NULL; LARGE_INTEGER Offset; PFATX_DIR_ENTRY pDirEntry; NTSTATUS Status; DPRINT("vfatRenameEntry(%p, %p, %wZ, %d)\n", DeviceExt, pFcb, FileName, CaseChangeOnly); if (pFcb->Flags & FCB_IS_FATX_ENTRY) { VFAT_DIRENTRY_CONTEXT DirContext; /* Open associated dir entry */ StartIndex = pFcb->startIndex; Offset.u.HighPart = 0; Offset.u.LowPart = (StartIndex * sizeof(FATX_DIR_ENTRY) / PAGE_SIZE) * PAGE_SIZE; _SEH2_TRY { CcPinRead(pFcb->parentFcb->FileObject, &Offset, PAGE_SIZE, PIN_WAIT, &Context, (PVOID*)&pDirEntry); } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { DPRINT1("CcPinRead(Offset %x:%x, Length %d) failed\n", Offset.u.HighPart, Offset.u.LowPart, PAGE_SIZE); _SEH2_YIELD(return _SEH2_GetExceptionCode()); } _SEH2_END; pDirEntry = &pDirEntry[StartIndex % (PAGE_SIZE / sizeof(FATX_DIR_ENTRY))]; /* Set file name */ NameA.Buffer = (PCHAR)pDirEntry->Filename; NameA.Length = 0; NameA.MaximumLength = 42; RtlUnicodeStringToOemString(&NameA, FileName, FALSE); pDirEntry->FilenameLength = (unsigned char)NameA.Length; /* Update FCB */ DirContext.ShortNameU.Length = 0; DirContext.ShortNameU.MaximumLength = 0; DirContext.ShortNameU.Buffer = NULL; DirContext.LongNameU = *FileName; DirContext.DirEntry.FatX = *pDirEntry; CcSetDirtyPinnedData(Context, NULL); CcUnpinData(Context); Status = vfatUpdateFCB(DeviceExt, pFcb, &DirContext, pFcb->parentFcb); if (NT_SUCCESS(Status)) { CcPurgeCacheSection(&pFcb->parentFcb->SectionObjectPointers, NULL, 0, FALSE); } return Status; }
/* * @implemented */ BOOL WINAPI GetVolumeInformationA(IN LPCSTR lpRootPathName, IN LPSTR lpVolumeNameBuffer, IN DWORD nVolumeNameSize, OUT LPDWORD lpVolumeSerialNumber OPTIONAL, OUT LPDWORD lpMaximumComponentLength OPTIONAL, OUT LPDWORD lpFileSystemFlags OPTIONAL, OUT LPSTR lpFileSystemNameBuffer OPTIONAL, IN DWORD nFileSystemNameSize) { UNICODE_STRING FileSystemNameU; UNICODE_STRING VolumeNameU = { 0, 0, NULL }; ANSI_STRING VolumeName; ANSI_STRING FileSystemName; PWCHAR RootPathNameW; BOOL Result; if (!(RootPathNameW = FilenameA2W(lpRootPathName, FALSE))) return FALSE; if (lpVolumeNameBuffer) { VolumeNameU.MaximumLength = (USHORT)nVolumeNameSize * sizeof(WCHAR); VolumeNameU.Buffer = RtlAllocateHeap (RtlGetProcessHeap (), 0, VolumeNameU.MaximumLength); if (VolumeNameU.Buffer == NULL) { goto FailNoMem; } } if (lpFileSystemNameBuffer) { FileSystemNameU.Length = 0; FileSystemNameU.MaximumLength = (USHORT)nFileSystemNameSize * sizeof(WCHAR); FileSystemNameU.Buffer = RtlAllocateHeap (RtlGetProcessHeap (), 0, FileSystemNameU.MaximumLength); if (FileSystemNameU.Buffer == NULL) { if (VolumeNameU.Buffer != NULL) { RtlFreeHeap(RtlGetProcessHeap(), 0, VolumeNameU.Buffer); } FailNoMem: SetLastError(ERROR_NOT_ENOUGH_MEMORY); return FALSE; } } Result = GetVolumeInformationW (RootPathNameW, lpVolumeNameBuffer ? VolumeNameU.Buffer : NULL, nVolumeNameSize, lpVolumeSerialNumber, lpMaximumComponentLength, lpFileSystemFlags, lpFileSystemNameBuffer ? FileSystemNameU.Buffer : NULL, nFileSystemNameSize); if (Result) { if (lpVolumeNameBuffer) { VolumeNameU.Length = wcslen(VolumeNameU.Buffer) * sizeof(WCHAR); VolumeName.Length = 0; VolumeName.MaximumLength = (USHORT)nVolumeNameSize; VolumeName.Buffer = lpVolumeNameBuffer; } if (lpFileSystemNameBuffer) { FileSystemNameU.Length = wcslen(FileSystemNameU.Buffer) * sizeof(WCHAR); FileSystemName.Length = 0; FileSystemName.MaximumLength = (USHORT)nFileSystemNameSize; FileSystemName.Buffer = lpFileSystemNameBuffer; } /* convert unicode strings to ansi (or oem) */ if (bIsFileApiAnsi) { if (lpVolumeNameBuffer) { RtlUnicodeStringToAnsiString (&VolumeName, &VolumeNameU, FALSE); } if (lpFileSystemNameBuffer) { RtlUnicodeStringToAnsiString (&FileSystemName, &FileSystemNameU, FALSE); } } else { if (lpVolumeNameBuffer) { RtlUnicodeStringToOemString (&VolumeName, &VolumeNameU, FALSE); } if (lpFileSystemNameBuffer) { RtlUnicodeStringToOemString (&FileSystemName, &FileSystemNameU, FALSE); } } } if (lpVolumeNameBuffer) { RtlFreeHeap (RtlGetProcessHeap (), 0, VolumeNameU.Buffer); } if (lpFileSystemNameBuffer) { RtlFreeHeap (RtlGetProcessHeap (), 0, FileSystemNameU.Buffer); } return Result; }