2019-04-12 20:32:40 +00:00
|
|
|
/*
|
|
|
|
* nt_io.c --- This is the Nt I/O interface to the I/O manager.
|
|
|
|
*
|
|
|
|
* Implements a one-block write-through cache.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, 1994, 1995 Theodore Ts'o.
|
|
|
|
* Copyright (C) 1998 Andrey Shedel <andreys@ns.cr.cyco.com>
|
|
|
|
* Copyright (C) 2018-2019 Pete Batard <pete@akeo.ie>
|
|
|
|
*
|
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the GNU Library
|
|
|
|
* General Public License, version 2.
|
|
|
|
* %End-Header%
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winternl.h>
|
2019-04-13 13:52:29 +00:00
|
|
|
#include <assert.h>
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "ext2fs.h"
|
|
|
|
#include "rufus.h"
|
|
|
|
#include "msapi_utf8.h"
|
|
|
|
|
|
|
|
extern char* NtStatusError(NTSTATUS Status);
|
2019-05-02 14:41:42 +00:00
|
|
|
static DWORD LastWinError = 0;
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
PF_TYPE_DECL(NTAPI, ULONG, RtlNtStatusToDosError, (NTSTATUS));
|
|
|
|
PF_TYPE_DECL(NTAPI, NTSTATUS, NtClose, (HANDLE));
|
|
|
|
PF_TYPE_DECL(NTAPI, NTSTATUS, NtOpenFile, (PHANDLE, ACCESS_MASK, POBJECT_ATTRIBUTES, PIO_STATUS_BLOCK, ULONG, ULONG));
|
|
|
|
PF_TYPE_DECL(NTAPI, NTSTATUS, NtFlushBuffersFile, (HANDLE, PIO_STATUS_BLOCK));
|
|
|
|
PF_TYPE_DECL(NTAPI, NTSTATUS, NtReadFile, (HANDLE, HANDLE, PIO_APC_ROUTINE, PVOID, PIO_STATUS_BLOCK, PVOID, ULONG, PLARGE_INTEGER, PULONG));
|
|
|
|
PF_TYPE_DECL(NTAPI, NTSTATUS, NtWriteFile, (HANDLE, HANDLE, PIO_APC_ROUTINE, PVOID, PIO_STATUS_BLOCK, PVOID, ULONG, PLARGE_INTEGER, PULONG));
|
|
|
|
PF_TYPE_DECL(NTAPI, NTSTATUS, NtDeviceIoControlFile, (HANDLE, HANDLE, PIO_APC_ROUTINE, PVOID, PIO_STATUS_BLOCK, ULONG, PVOID, ULONG, PVOID, ULONG));
|
|
|
|
PF_TYPE_DECL(NTAPI, NTSTATUS, NtFsControlFile, (HANDLE, HANDLE, PIO_APC_ROUTINE, PVOID, PIO_STATUS_BLOCK, ULONG, PVOID, ULONG, PVOID, ULONG));
|
|
|
|
PF_TYPE_DECL(NTAPI, NTSTATUS, NtDelayExecution, (BOOLEAN, PLARGE_INTEGER));
|
|
|
|
|
|
|
|
#define ARGUMENT_PRESENT(ArgumentPointer) ((CHAR *)((ULONG_PTR)(ArgumentPointer)) != (CHAR *)(NULL))
|
|
|
|
|
|
|
|
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
|
|
|
|
#define STATUS_ACCESS_DENIED ((NTSTATUS)0xC0000022L)
|
|
|
|
#define STATUS_INVALID_DEVICE_REQUEST ((NTSTATUS)0xC0000010L)
|
|
|
|
|
|
|
|
#define BooleanFlagOn(Flags, SingleFlag) ((BOOLEAN)((((Flags) & (SingleFlag)) != 0)))
|
|
|
|
|
2019-04-13 13:52:29 +00:00
|
|
|
#define EXT2_ET_MAGIC_NT_IO_CHANNEL 0x10ed
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
// Private data block
|
|
|
|
typedef struct _NT_PRIVATE_DATA {
|
|
|
|
int magic;
|
|
|
|
HANDLE handle;
|
|
|
|
int flags;
|
|
|
|
char* buffer;
|
|
|
|
__u32 buffer_block_number;
|
|
|
|
ULONG buffer_size;
|
|
|
|
BOOLEAN read_only;
|
|
|
|
BOOLEAN written;
|
2019-09-14 22:33:46 +00:00
|
|
|
// Used by Rufus
|
|
|
|
__u64 offset;
|
|
|
|
__u64 size;
|
2019-04-12 20:32:40 +00:00
|
|
|
} NT_PRIVATE_DATA, *PNT_PRIVATE_DATA;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Standard interface prototypes
|
|
|
|
//
|
|
|
|
|
|
|
|
static errcode_t nt_open(const char *name, int flags, io_channel *channel);
|
|
|
|
static errcode_t nt_close(io_channel channel);
|
|
|
|
static errcode_t nt_set_blksize(io_channel channel, int blksize);
|
|
|
|
static errcode_t nt_read_blk(io_channel channel, unsigned long block, int count, void *data);
|
[extfs] fix inodes not being initialized above 4 GB
* So, as it happens, when assigning the product of two 32-bit variables into a 64-bit one,
compilers default to being *DUMB* and, against all reasonable expectations, do not perform
that multiplication as a 64-bit operation (even when the code is compiled as x64). Wow,
that's really great decision making by compiler designers if I ever saw some... Whoever
decided that C developers would much rather want truncation and 32-bit overflows, instead
of the expected *LOGICAL* behaviour of conducting arithmetic operations as 64-bit when the
result will be assigned to a 64-bit variable, need to be condemned to a lifetime of trying
to help elderly folks trying to conduct simple computing tasks as a punishment...
Anyhoo, nt_write_blk()'s offset.QuadPart = block * channel->block_size + nt_data->offset
was overflowing 32-bit as soon as block * channel->block_size went over the 4 GB mark,
with the disastrous results one can expect. Considering that this is code we practically
lifted verbatim from e2fsprogs, I guess e2fsprogs' NT I/O manager was never properly
tested with anything larger than a 4 GB. Awesome!
* We fix the above by doing what unix_io.c does and setting the 32-bit read/write_blk()
calls to be wrappers around their 64-bit counterpart (since, once you deal with a 64-bit
block variable, the computation is conducted as 64-bit).
* Also remove a bunch of stuff we don't need from config.h
* Closes #1396
2020-02-12 16:26:22 +00:00
|
|
|
static errcode_t nt_read_blk64(io_channel channel, unsigned long long block, int count, void* data);
|
2019-04-12 20:32:40 +00:00
|
|
|
static errcode_t nt_write_blk(io_channel channel, unsigned long block, int count, const void *data);
|
[extfs] fix inodes not being initialized above 4 GB
* So, as it happens, when assigning the product of two 32-bit variables into a 64-bit one,
compilers default to being *DUMB* and, against all reasonable expectations, do not perform
that multiplication as a 64-bit operation (even when the code is compiled as x64). Wow,
that's really great decision making by compiler designers if I ever saw some... Whoever
decided that C developers would much rather want truncation and 32-bit overflows, instead
of the expected *LOGICAL* behaviour of conducting arithmetic operations as 64-bit when the
result will be assigned to a 64-bit variable, need to be condemned to a lifetime of trying
to help elderly folks trying to conduct simple computing tasks as a punishment...
Anyhoo, nt_write_blk()'s offset.QuadPart = block * channel->block_size + nt_data->offset
was overflowing 32-bit as soon as block * channel->block_size went over the 4 GB mark,
with the disastrous results one can expect. Considering that this is code we practically
lifted verbatim from e2fsprogs, I guess e2fsprogs' NT I/O manager was never properly
tested with anything larger than a 4 GB. Awesome!
* We fix the above by doing what unix_io.c does and setting the 32-bit read/write_blk()
calls to be wrappers around their 64-bit counterpart (since, once you deal with a 64-bit
block variable, the computation is conducted as 64-bit).
* Also remove a bunch of stuff we don't need from config.h
* Closes #1396
2020-02-12 16:26:22 +00:00
|
|
|
static errcode_t nt_write_blk64(io_channel channel, unsigned long long block, int count, const void* data);
|
2019-04-12 20:32:40 +00:00
|
|
|
static errcode_t nt_flush(io_channel channel);
|
|
|
|
|
|
|
|
static struct struct_io_manager struct_nt_manager = {
|
|
|
|
.magic = EXT2_ET_MAGIC_IO_MANAGER,
|
|
|
|
.name = "NT I/O Manager",
|
|
|
|
.open = nt_open,
|
|
|
|
.close = nt_close,
|
|
|
|
.set_blksize = nt_set_blksize,
|
|
|
|
.read_blk = nt_read_blk,
|
[extfs] fix inodes not being initialized above 4 GB
* So, as it happens, when assigning the product of two 32-bit variables into a 64-bit one,
compilers default to being *DUMB* and, against all reasonable expectations, do not perform
that multiplication as a 64-bit operation (even when the code is compiled as x64). Wow,
that's really great decision making by compiler designers if I ever saw some... Whoever
decided that C developers would much rather want truncation and 32-bit overflows, instead
of the expected *LOGICAL* behaviour of conducting arithmetic operations as 64-bit when the
result will be assigned to a 64-bit variable, need to be condemned to a lifetime of trying
to help elderly folks trying to conduct simple computing tasks as a punishment...
Anyhoo, nt_write_blk()'s offset.QuadPart = block * channel->block_size + nt_data->offset
was overflowing 32-bit as soon as block * channel->block_size went over the 4 GB mark,
with the disastrous results one can expect. Considering that this is code we practically
lifted verbatim from e2fsprogs, I guess e2fsprogs' NT I/O manager was never properly
tested with anything larger than a 4 GB. Awesome!
* We fix the above by doing what unix_io.c does and setting the 32-bit read/write_blk()
calls to be wrappers around their 64-bit counterpart (since, once you deal with a 64-bit
block variable, the computation is conducted as 64-bit).
* Also remove a bunch of stuff we don't need from config.h
* Closes #1396
2020-02-12 16:26:22 +00:00
|
|
|
.read_blk64 = nt_read_blk64,
|
2019-04-12 20:32:40 +00:00
|
|
|
.write_blk = nt_write_blk,
|
[extfs] fix inodes not being initialized above 4 GB
* So, as it happens, when assigning the product of two 32-bit variables into a 64-bit one,
compilers default to being *DUMB* and, against all reasonable expectations, do not perform
that multiplication as a 64-bit operation (even when the code is compiled as x64). Wow,
that's really great decision making by compiler designers if I ever saw some... Whoever
decided that C developers would much rather want truncation and 32-bit overflows, instead
of the expected *LOGICAL* behaviour of conducting arithmetic operations as 64-bit when the
result will be assigned to a 64-bit variable, need to be condemned to a lifetime of trying
to help elderly folks trying to conduct simple computing tasks as a punishment...
Anyhoo, nt_write_blk()'s offset.QuadPart = block * channel->block_size + nt_data->offset
was overflowing 32-bit as soon as block * channel->block_size went over the 4 GB mark,
with the disastrous results one can expect. Considering that this is code we practically
lifted verbatim from e2fsprogs, I guess e2fsprogs' NT I/O manager was never properly
tested with anything larger than a 4 GB. Awesome!
* We fix the above by doing what unix_io.c does and setting the 32-bit read/write_blk()
calls to be wrappers around their 64-bit counterpart (since, once you deal with a 64-bit
block variable, the computation is conducted as 64-bit).
* Also remove a bunch of stuff we don't need from config.h
* Closes #1396
2020-02-12 16:26:22 +00:00
|
|
|
.write_blk64 = nt_write_blk64,
|
2019-04-12 20:32:40 +00:00
|
|
|
.flush = nt_flush
|
|
|
|
};
|
|
|
|
|
|
|
|
io_manager nt_io_manager(void)
|
|
|
|
{
|
|
|
|
return &struct_nt_manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert Win32 errors to unix errno
|
|
|
|
typedef struct {
|
|
|
|
ULONG WinError;
|
|
|
|
int errnocode;
|
2019-05-02 14:41:42 +00:00
|
|
|
} ERROR_ENTRY;
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
static ERROR_ENTRY ErrorTable[] = {
|
|
|
|
{ ERROR_INVALID_FUNCTION, EINVAL },
|
|
|
|
{ ERROR_FILE_NOT_FOUND, ENOENT },
|
|
|
|
{ ERROR_PATH_NOT_FOUND, ENOENT },
|
|
|
|
{ ERROR_TOO_MANY_OPEN_FILES, EMFILE },
|
|
|
|
{ ERROR_ACCESS_DENIED, EACCES },
|
|
|
|
{ ERROR_INVALID_HANDLE, EBADF },
|
|
|
|
{ ERROR_ARENA_TRASHED, ENOMEM },
|
|
|
|
{ ERROR_NOT_ENOUGH_MEMORY, ENOMEM },
|
|
|
|
{ ERROR_INVALID_BLOCK, ENOMEM },
|
|
|
|
{ ERROR_BAD_ENVIRONMENT, E2BIG },
|
|
|
|
{ ERROR_BAD_FORMAT, ENOEXEC },
|
|
|
|
{ ERROR_INVALID_ACCESS, EINVAL },
|
|
|
|
{ ERROR_INVALID_DATA, EINVAL },
|
|
|
|
{ ERROR_INVALID_DRIVE, ENOENT },
|
|
|
|
{ ERROR_CURRENT_DIRECTORY, EACCES },
|
|
|
|
{ ERROR_NOT_SAME_DEVICE, EXDEV },
|
|
|
|
{ ERROR_NO_MORE_FILES, ENOENT },
|
|
|
|
{ ERROR_LOCK_VIOLATION, EACCES },
|
|
|
|
{ ERROR_BAD_NETPATH, ENOENT },
|
|
|
|
{ ERROR_NETWORK_ACCESS_DENIED, EACCES },
|
|
|
|
{ ERROR_BAD_NET_NAME, ENOENT },
|
|
|
|
{ ERROR_FILE_EXISTS, EEXIST },
|
|
|
|
{ ERROR_CANNOT_MAKE, EACCES },
|
|
|
|
{ ERROR_FAIL_I24, EACCES },
|
|
|
|
{ ERROR_INVALID_PARAMETER, EINVAL },
|
|
|
|
{ ERROR_NO_PROC_SLOTS, EAGAIN },
|
|
|
|
{ ERROR_DRIVE_LOCKED, EACCES },
|
|
|
|
{ ERROR_BROKEN_PIPE, EPIPE },
|
|
|
|
{ ERROR_DISK_FULL, ENOSPC },
|
|
|
|
{ ERROR_INVALID_TARGET_HANDLE, EBADF },
|
|
|
|
{ ERROR_INVALID_HANDLE, EINVAL },
|
|
|
|
{ ERROR_WAIT_NO_CHILDREN, ECHILD },
|
|
|
|
{ ERROR_CHILD_NOT_COMPLETE, ECHILD },
|
|
|
|
{ ERROR_DIRECT_ACCESS_HANDLE, EBADF },
|
|
|
|
{ ERROR_NEGATIVE_SEEK, EINVAL },
|
|
|
|
{ ERROR_SEEK_ON_DEVICE, EACCES },
|
|
|
|
{ ERROR_DIR_NOT_EMPTY, ENOTEMPTY },
|
|
|
|
{ ERROR_NOT_LOCKED, EACCES },
|
|
|
|
{ ERROR_BAD_PATHNAME, ENOENT },
|
|
|
|
{ ERROR_MAX_THRDS_REACHED, EAGAIN },
|
|
|
|
{ ERROR_LOCK_FAILED, EACCES },
|
|
|
|
{ ERROR_ALREADY_EXISTS, EEXIST },
|
|
|
|
{ ERROR_FILENAME_EXCED_RANGE, ENOENT },
|
|
|
|
{ ERROR_NESTING_NOT_ALLOWED, EAGAIN },
|
|
|
|
{ ERROR_NOT_ENOUGH_QUOTA, ENOMEM }
|
|
|
|
};
|
|
|
|
|
2019-04-13 13:52:29 +00:00
|
|
|
static unsigned _MapDosError(IN ULONG WinError)
|
2019-04-12 20:32:40 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2019-05-02 14:41:42 +00:00
|
|
|
LastWinError = WinError;
|
2019-04-12 20:32:40 +00:00
|
|
|
for (i = 0; i < (sizeof(ErrorTable)/sizeof(ErrorTable[0])); ++i) {
|
|
|
|
if (WinError == ErrorTable[i].WinError) {
|
|
|
|
return ErrorTable[i].errnocode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not in table. Check ranges
|
|
|
|
if ((WinError >= ERROR_WRITE_PROTECT) && (WinError <= ERROR_SHARING_BUFFER_EXCEEDED))
|
|
|
|
return EACCES;
|
|
|
|
else if ((WinError >= ERROR_INVALID_STARTING_CODESEG) && (WinError <= ERROR_INFLOOP_IN_RELOC_CHAIN))
|
|
|
|
return ENOEXEC;
|
|
|
|
else
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Map NT status to dos error.
|
|
|
|
static __inline unsigned _MapNtStatus(IN NTSTATUS Status)
|
|
|
|
{
|
|
|
|
PF_INIT(RtlNtStatusToDosError, Ntdll);
|
|
|
|
return (pfRtlNtStatusToDosError == NULL) ? EFAULT: _MapDosError(pfRtlNtStatusToDosError(Status));
|
|
|
|
}
|
|
|
|
|
2019-05-02 14:41:42 +00:00
|
|
|
// Return the last Windows Error
|
|
|
|
DWORD ext2_last_winerror(DWORD default_error)
|
|
|
|
{
|
|
|
|
return ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | (LastWinError ? LastWinError : default_error);
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:32:40 +00:00
|
|
|
//
|
|
|
|
// Helper functions
|
|
|
|
//
|
|
|
|
static NTSTATUS _OpenNtName(IN PCSTR Name, IN BOOLEAN Readonly, OUT PHANDLE Handle, OUT PBOOLEAN OpenedReadonly OPTIONAL)
|
|
|
|
{
|
|
|
|
UNICODE_STRING UnicodeString;
|
|
|
|
WCHAR Buffer[512];
|
|
|
|
NTSTATUS Status = EFAULT;
|
|
|
|
OBJECT_ATTRIBUTES ObjectAttributes;
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
PF_INIT(NtDelayExecution, Ntdll);
|
|
|
|
PF_INIT_OR_OUT(NtOpenFile, Ntdll);
|
|
|
|
|
|
|
|
// Make Unicode name from input string
|
2019-04-13 13:52:29 +00:00
|
|
|
utf8_to_wchar_no_alloc(Name, Buffer, ARRAYSIZE(Buffer));
|
2019-04-12 20:32:40 +00:00
|
|
|
UnicodeString.Buffer = Buffer;
|
|
|
|
UnicodeString.Length = (USHORT) wcslen(Buffer) * 2;
|
|
|
|
UnicodeString.MaximumLength = sizeof(Buffer); // in bytes!!!
|
|
|
|
|
|
|
|
// Initialize object
|
2019-09-14 22:33:46 +00:00
|
|
|
InitializeObjectAttributes(&ObjectAttributes, &UnicodeString, OBJ_CASE_INSENSITIVE, NULL, NULL);
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
// Try to open it in initial mode
|
|
|
|
if (ARGUMENT_PRESENT(OpenedReadonly))
|
|
|
|
*OpenedReadonly = Readonly;
|
|
|
|
|
|
|
|
Status = pfNtOpenFile(Handle, SYNCHRONIZE | FILE_READ_DATA | (Readonly ? 0 : FILE_WRITE_DATA),
|
|
|
|
&ObjectAttributes, &IoStatusBlock, FILE_SHARE_WRITE | FILE_SHARE_READ,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
// Maybe was just mounted? wait 0.5 sec and retry.
|
|
|
|
LARGE_INTEGER Interval;
|
|
|
|
Interval.QuadPart = -5000000; // 0.5 sec. from now
|
2019-04-13 13:52:29 +00:00
|
|
|
pfNtDelayExecution(FALSE, &Interval);
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
Status = pfNtOpenFile(Handle, SYNCHRONIZE | FILE_READ_DATA | (Readonly ? 0 : FILE_WRITE_DATA),
|
|
|
|
&ObjectAttributes, &IoStatusBlock, FILE_SHARE_WRITE | FILE_SHARE_READ,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT);
|
|
|
|
|
|
|
|
// Try to satisfy mode
|
|
|
|
if ((Status == STATUS_ACCESS_DENIED) && !Readonly) {
|
|
|
|
if (ARGUMENT_PRESENT(OpenedReadonly))
|
|
|
|
*OpenedReadonly = TRUE;
|
|
|
|
|
|
|
|
Status = pfNtOpenFile(Handle, SYNCHRONIZE | FILE_READ_DATA, &ObjectAttributes,
|
|
|
|
&IoStatusBlock, FILE_SHARE_WRITE | FILE_SHARE_READ,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS _OpenDriveLetter(IN CHAR Letter, IN BOOLEAN ReadOnly, OUT PHANDLE Handle, OUT PBOOLEAN OpenedReadonly OPTIONAL)
|
|
|
|
{
|
|
|
|
CHAR Buffer[100];
|
|
|
|
sprintf(Buffer, "\\DosDevices\\%c:", Letter);
|
|
|
|
return _OpenNtName(Buffer, ReadOnly, Handle, OpenedReadonly);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline NTSTATUS _FlushDrive(IN HANDLE Handle)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
PF_INIT(NtFlushBuffersFile, NtDll);
|
|
|
|
return (pfNtFlushBuffersFile == NULL) ? STATUS_DLL_NOT_FOUND : pfNtFlushBuffersFile(Handle, &IoStatusBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static __inline NTSTATUS _LockDrive(IN HANDLE Handle)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
PF_INIT(NtFsControlFile, NtDll);
|
|
|
|
return (pfNtFsControlFile == NULL) ? STATUS_DLL_NOT_FOUND : pfNtFsControlFile(Handle, 0, 0, 0, &IoStatusBlock, FSCTL_LOCK_VOLUME, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static __inline NTSTATUS _UnlockDrive(IN HANDLE Handle)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
PF_INIT(NtFsControlFile, NtDll);
|
|
|
|
return (pfNtFsControlFile == NULL) ? STATUS_DLL_NOT_FOUND : pfNtFsControlFile(Handle, 0, 0, 0, &IoStatusBlock, FSCTL_UNLOCK_VOLUME, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline NTSTATUS _DismountDrive(IN HANDLE Handle)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
PF_INIT(NtFsControlFile, NtDll);
|
|
|
|
return (pfNtFsControlFile == NULL) ? STATUS_DLL_NOT_FOUND : pfNtFsControlFile(Handle, 0, 0, 0, &IoStatusBlock, FSCTL_DISMOUNT_VOLUME, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline BOOLEAN _IsMounted(IN HANDLE Handle)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
PF_INIT(NtFsControlFile, NtDll);
|
2020-02-17 23:10:58 +00:00
|
|
|
return (pfNtFsControlFile == NULL) ? STATUS_DLL_NOT_FOUND :
|
|
|
|
(BOOLEAN)(pfNtFsControlFile(Handle, 0, 0, 0, &IoStatusBlock, FSCTL_IS_VOLUME_MOUNTED, 0, 0, 0, 0) == STATUS_SUCCESS);
|
2019-04-12 20:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline NTSTATUS _CloseDisk(IN HANDLE Handle)
|
|
|
|
{
|
|
|
|
PF_INIT(NtClose, Ntdll);
|
|
|
|
return (pfNtClose == NULL) ? STATUS_DLL_NOT_FOUND : pfNtClose(Handle);
|
|
|
|
}
|
|
|
|
|
2019-09-14 22:33:46 +00:00
|
|
|
static PCSTR _NormalizeDeviceName(IN PCSTR Device, IN PSTR NormalizedDeviceNameBuffer, OUT __u64 *Offset, OUT __u64 *Size)
|
2019-04-12 20:32:40 +00:00
|
|
|
{
|
2019-09-14 22:33:46 +00:00
|
|
|
*Offset = *Size = 0ULL;
|
2019-04-12 20:32:40 +00:00
|
|
|
// Convert non NT paths to NT
|
|
|
|
if (Device[0] == '\\') {
|
|
|
|
if ((strlen(Device) < 4) || (Device[3] != '\\'))
|
|
|
|
return Device;
|
2019-09-14 22:33:46 +00:00
|
|
|
// Handle custom paths of the form "<Physical> <Offset> <Size>" used by Rufus to
|
|
|
|
// enable multi-partition access on removable devices, for pre 1703 platforms.
|
|
|
|
if (sscanf(Device, "%s %I64u %I64u", NormalizedDeviceNameBuffer, Offset, Size) < 1)
|
|
|
|
return NULL;
|
2019-04-12 20:32:40 +00:00
|
|
|
if ((NormalizedDeviceNameBuffer[1] == '\\') || (NormalizedDeviceNameBuffer[1] == '.'))
|
|
|
|
NormalizedDeviceNameBuffer[1] = '?';
|
|
|
|
if (NormalizedDeviceNameBuffer[2] == '.')
|
|
|
|
NormalizedDeviceNameBuffer[2] = '?';
|
|
|
|
return NormalizedDeviceNameBuffer;
|
|
|
|
}
|
|
|
|
|
2019-04-13 16:17:23 +00:00
|
|
|
// Don't allow the conversion of non absolute paths.
|
2019-04-12 20:32:40 +00:00
|
|
|
// Too easy to get a C:\ drive altered on a mishap otherwise...
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID _GetDeviceSize(IN HANDLE h, OUT unsigned __int64 *FsSize)
|
|
|
|
{
|
|
|
|
PARTITION_INFORMATION_EX pi;
|
|
|
|
DISK_GEOMETRY_EX gi;
|
|
|
|
NTSTATUS Status;
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
|
|
|
|
*FsSize = 0;
|
|
|
|
PF_INIT(NtDeviceIoControlFile, NtDll);
|
|
|
|
if (pfNtDeviceIoControlFile == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RtlZeroMemory(&pi, sizeof(pi));
|
|
|
|
Status = pfNtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock,
|
|
|
|
IOCTL_DISK_GET_PARTITION_INFO_EX,
|
|
|
|
&pi, sizeof(pi), &pi, sizeof(pi));
|
|
|
|
if (NT_SUCCESS(Status)) {
|
|
|
|
*FsSize = pi.PartitionLength.QuadPart;
|
|
|
|
} else if (Status == STATUS_INVALID_DEVICE_REQUEST) {
|
|
|
|
// No partitions: Try a drive geometry request
|
|
|
|
RtlZeroMemory(&gi, sizeof(gi));
|
|
|
|
|
|
|
|
Status = pfNtDeviceIoControlFile(h, NULL, NULL, NULL, &IoStatusBlock,
|
|
|
|
IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
|
|
|
&gi, sizeof(gi), &gi, sizeof(gi));
|
|
|
|
|
2019-04-15 16:04:39 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
2019-04-12 20:32:40 +00:00
|
|
|
*FsSize = gi.DiskSize.QuadPart;
|
|
|
|
} else if (Status == STATUS_INVALID_PARAMETER) {
|
|
|
|
// Possibly a straight image file
|
|
|
|
if (GetFileSizeEx(h, &li))
|
|
|
|
*FsSize = li.QuadPart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 22:33:46 +00:00
|
|
|
static BOOLEAN _Ext2OpenDevice(IN PCSTR Name, IN BOOLEAN ReadOnly, OUT PHANDLE Handle,
|
|
|
|
OUT __u64 *Offset, OUT __u64 *Size, OUT PBOOLEAN OpenedReadonly OPTIONAL, OUT errcode_t *Errno OPTIONAL)
|
2019-04-12 20:32:40 +00:00
|
|
|
{
|
|
|
|
CHAR NormalizedDeviceName[512];
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
2019-05-02 14:41:42 +00:00
|
|
|
LastWinError = 0;
|
2019-04-12 20:32:40 +00:00
|
|
|
if (Name == NULL) {
|
2019-05-02 14:41:42 +00:00
|
|
|
LastWinError = ERROR_INVALID_PARAMETER;
|
2019-04-12 20:32:40 +00:00
|
|
|
if (ARGUMENT_PRESENT(Errno))
|
|
|
|
*Errno = ENOENT;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((((*Name) | 0x20) >= 'a') && (((*Name) | 0x20) <= 'z') &&
|
|
|
|
(':' == *(Name + 1)) && ('\0' == *(Name + 2))) {
|
|
|
|
Status = _OpenDriveLetter(*Name, ReadOnly, Handle, OpenedReadonly);
|
|
|
|
} else {
|
2019-09-14 22:33:46 +00:00
|
|
|
Name = _NormalizeDeviceName(Name, NormalizedDeviceName, Offset, Size);
|
2019-04-12 20:32:40 +00:00
|
|
|
if (Name == NULL) {
|
2019-05-02 14:41:42 +00:00
|
|
|
LastWinError = ERROR_INVALID_PARAMETER;
|
2019-04-12 20:32:40 +00:00
|
|
|
if (ARGUMENT_PRESENT(Errno))
|
|
|
|
*Errno = ENOENT;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = _OpenNtName(Name, ReadOnly, Handle, OpenedReadonly);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
if (ARGUMENT_PRESENT(Errno))
|
|
|
|
*Errno = _MapNtStatus(Status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-05-02 14:41:42 +00:00
|
|
|
static BOOLEAN _BlockIo(IN HANDLE Handle, IN LARGE_INTEGER Offset, IN ULONG Bytes, IN OUT PCHAR Buffer, IN BOOLEAN Read, OUT errcode_t *Errno OPTIONAL)
|
2019-04-12 20:32:40 +00:00
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
NTSTATUS Status = STATUS_DLL_NOT_FOUND;
|
|
|
|
PF_INIT_OR_OUT(NtReadFile, NtDll);
|
|
|
|
PF_INIT_OR_OUT(NtWriteFile, NtDll);
|
|
|
|
|
|
|
|
// Should be aligned
|
2019-04-13 13:52:29 +00:00
|
|
|
assert((Bytes % 512) == 0);
|
|
|
|
assert((Offset.LowPart % 512) == 0);
|
2019-04-12 20:32:40 +00:00
|
|
|
|
2019-05-02 14:41:42 +00:00
|
|
|
LastWinError = 0;
|
2019-04-12 20:32:40 +00:00
|
|
|
// Perform io
|
|
|
|
if(Read) {
|
|
|
|
Status = pfNtReadFile(Handle, NULL, NULL, NULL,
|
|
|
|
&IoStatusBlock, Buffer, Bytes, &Offset, NULL);
|
|
|
|
} else {
|
|
|
|
Status = pfNtWriteFile(Handle, NULL, NULL, NULL,
|
|
|
|
&IoStatusBlock, Buffer, Bytes, &Offset, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2019-05-02 14:41:42 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
if (ARGUMENT_PRESENT(Errno))
|
|
|
|
*Errno = _MapNtStatus(Status);
|
|
|
|
return FALSE;
|
2019-04-12 20:32:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 14:41:42 +00:00
|
|
|
if (ARGUMENT_PRESENT(Errno))
|
|
|
|
*Errno = 0;
|
|
|
|
return TRUE;
|
2019-04-12 20:32:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 14:41:42 +00:00
|
|
|
static BOOLEAN _RawWrite(IN HANDLE Handle, IN LARGE_INTEGER Offset, IN ULONG Bytes, OUT const CHAR* Buffer, OUT errcode_t* Errno)
|
2019-04-12 20:32:40 +00:00
|
|
|
{
|
|
|
|
return _BlockIo(Handle, Offset, Bytes, (PCHAR)Buffer, FALSE, Errno);
|
|
|
|
}
|
|
|
|
|
2019-05-02 14:41:42 +00:00
|
|
|
static BOOLEAN _RawRead(IN HANDLE Handle, IN LARGE_INTEGER Offset, IN ULONG Bytes, IN PCHAR Buffer, OUT errcode_t* Errno)
|
2019-04-12 20:32:40 +00:00
|
|
|
{
|
|
|
|
return _BlockIo(Handle, Offset, Bytes, Buffer, TRUE, Errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOLEAN _SetPartType(IN HANDLE Handle, IN UCHAR Type)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
PF_INIT(NtDeviceIoControlFile, NtDll);
|
|
|
|
if (pfNtDeviceIoControlFile == NULL)
|
|
|
|
return FALSE;
|
|
|
|
return NT_SUCCESS(pfNtDeviceIoControlFile(Handle, NULL, NULL, NULL, &IoStatusBlock,
|
2019-04-13 13:52:29 +00:00
|
|
|
IOCTL_DISK_SET_PARTITION_INFO, &Type, sizeof(Type), NULL, 0));
|
2019-04-12 20:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Interface functions.
|
|
|
|
// Is_mounted is set to 1 if the device is mounted, 0 otherwise
|
|
|
|
//
|
2019-04-13 13:52:29 +00:00
|
|
|
errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
|
2019-04-12 20:32:40 +00:00
|
|
|
{
|
2019-05-02 14:41:42 +00:00
|
|
|
errcode_t errcode = 0;
|
2019-09-14 22:33:46 +00:00
|
|
|
__u64 Offset, Size;
|
2019-04-12 20:32:40 +00:00
|
|
|
HANDLE h;
|
|
|
|
BOOLEAN Readonly;
|
|
|
|
|
|
|
|
*mount_flags = 0;
|
|
|
|
|
2019-09-14 22:33:46 +00:00
|
|
|
if (!_Ext2OpenDevice(file, TRUE, &h, &Offset, &Size, &Readonly, &errcode))
|
2019-05-02 14:41:42 +00:00
|
|
|
return errcode;
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
*mount_flags &= _IsMounted(h) ? EXT2_MF_MOUNTED : 0;
|
|
|
|
_CloseDisk(h);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-15 16:04:39 +00:00
|
|
|
// Not implemented
|
|
|
|
errcode_t ext2fs_check_mount_point(const char *file, int *mount_flags, char *mtpt, int mtlen)
|
|
|
|
{
|
|
|
|
return EXT2_ET_OP_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:32:40 +00:00
|
|
|
// Returns the number of blocks in a partition
|
|
|
|
// Note: Do *NOT* be tempted to cache the device size according to the NT path as
|
|
|
|
// different removable devices (e.g. UFD) may be remounted under the same path.
|
2019-04-13 13:52:29 +00:00
|
|
|
errcode_t ext2fs_get_device_size2(const char *file, int blocksize, blk64_t *retblocks)
|
2019-04-12 20:32:40 +00:00
|
|
|
{
|
2019-09-14 22:33:46 +00:00
|
|
|
errcode_t errcode = 0;
|
|
|
|
__u64 Offset, Size = 0;
|
2019-04-12 20:32:40 +00:00
|
|
|
HANDLE h;
|
|
|
|
BOOLEAN Readonly;
|
|
|
|
|
2019-09-14 22:33:46 +00:00
|
|
|
if (!_Ext2OpenDevice(file, TRUE, &h, &Offset, &Size, &Readonly, &errcode))
|
2019-05-02 14:41:42 +00:00
|
|
|
return errcode;
|
2019-04-12 20:32:40 +00:00
|
|
|
|
2019-09-14 22:33:46 +00:00
|
|
|
if (Size == 0LL)
|
|
|
|
_GetDeviceSize(h, &Size);
|
2019-04-12 20:32:40 +00:00
|
|
|
_CloseDisk(h);
|
|
|
|
|
2019-09-14 22:33:46 +00:00
|
|
|
*retblocks = (blk64_t)(Size / blocksize);
|
2019-04-12 20:32:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Table elements
|
|
|
|
//
|
|
|
|
static errcode_t nt_open(const char *name, int flags, io_channel *channel)
|
|
|
|
{
|
|
|
|
io_channel io = NULL;
|
|
|
|
PNT_PRIVATE_DATA nt_data = NULL;
|
|
|
|
errcode_t errcode = 0;
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
return EXT2_ET_BAD_DEVICE_NAME;
|
|
|
|
|
2019-04-13 16:17:23 +00:00
|
|
|
// Allocate buffers
|
|
|
|
io = (io_channel) calloc(1, sizeof(struct struct_io_channel));
|
2019-04-12 20:32:40 +00:00
|
|
|
if (io == NULL) {
|
|
|
|
errcode = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-04-13 16:17:23 +00:00
|
|
|
io->name = calloc(strlen(name) + 1, 1);
|
|
|
|
if (io->name == NULL) {
|
|
|
|
errcode = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-04-12 20:32:40 +00:00
|
|
|
|
2019-04-13 16:17:23 +00:00
|
|
|
nt_data = (PNT_PRIVATE_DATA) calloc(1, sizeof(NT_PRIVATE_DATA));
|
2019-04-12 20:32:40 +00:00
|
|
|
if (nt_data == NULL) {
|
|
|
|
errcode = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-04-13 16:17:23 +00:00
|
|
|
nt_data->buffer = malloc(EXT2_MIN_BLOCK_SIZE);
|
|
|
|
if (nt_data->buffer == NULL) {
|
2019-04-12 20:32:40 +00:00
|
|
|
errcode = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-04-13 16:17:23 +00:00
|
|
|
// Initialize data
|
|
|
|
io->magic = EXT2_ET_MAGIC_IO_CHANNEL;
|
|
|
|
io->manager = nt_io_manager();
|
2019-04-12 20:32:40 +00:00
|
|
|
strcpy(io->name, name);
|
2019-04-13 16:17:23 +00:00
|
|
|
io->block_size = EXT2_MIN_BLOCK_SIZE;
|
2019-04-12 20:32:40 +00:00
|
|
|
io->refcount = 1;
|
|
|
|
|
|
|
|
nt_data->magic = EXT2_ET_MAGIC_NT_IO_CHANNEL;
|
|
|
|
nt_data->buffer_block_number = 0xffffffff;
|
2019-04-13 16:17:23 +00:00
|
|
|
nt_data->buffer_size = EXT2_MIN_BLOCK_SIZE;
|
|
|
|
io->private_data = nt_data;
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
// Open the device
|
2019-09-14 22:33:46 +00:00
|
|
|
if (!_Ext2OpenDevice(name, (BOOLEAN)!BooleanFlagOn(flags, EXT2_FLAG_RW), &nt_data->handle,
|
|
|
|
&nt_data->offset, &nt_data->size, &nt_data->read_only, &errcode)) {
|
2019-04-13 16:17:23 +00:00
|
|
|
if (!errcode)
|
|
|
|
errcode = EIO;
|
2019-04-12 20:32:40 +00:00
|
|
|
goto out;
|
2019-04-13 16:17:23 +00:00
|
|
|
}
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
// Done
|
|
|
|
*channel = io;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (errcode) {
|
|
|
|
if (io != NULL) {
|
|
|
|
free(io->name);
|
|
|
|
free(io);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nt_data != NULL) {
|
|
|
|
if (nt_data->handle != NULL) {
|
|
|
|
_UnlockDrive(nt_data->handle);
|
|
|
|
_CloseDisk(nt_data->handle);
|
|
|
|
}
|
|
|
|
free(nt_data->buffer);
|
|
|
|
free(nt_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static errcode_t nt_close(io_channel channel)
|
|
|
|
{
|
|
|
|
PNT_PRIVATE_DATA nt_data = NULL;
|
|
|
|
|
|
|
|
if (channel == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
|
|
|
|
nt_data = (PNT_PRIVATE_DATA) channel->private_data;
|
|
|
|
EXT2_CHECK_MAGIC(nt_data, EXT2_ET_MAGIC_NT_IO_CHANNEL);
|
|
|
|
|
|
|
|
if (--channel->refcount > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
free(channel->name);
|
|
|
|
free(channel);
|
|
|
|
|
|
|
|
if (nt_data != NULL) {
|
|
|
|
if (nt_data->handle != NULL)
|
|
|
|
CloseHandle(nt_data->handle);
|
|
|
|
free(nt_data->buffer);
|
|
|
|
free(nt_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static errcode_t nt_set_blksize(io_channel channel, int blksize)
|
|
|
|
{
|
|
|
|
PNT_PRIVATE_DATA nt_data = NULL;
|
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
|
|
|
|
nt_data = (PNT_PRIVATE_DATA) channel->private_data;
|
|
|
|
EXT2_CHECK_MAGIC(nt_data, EXT2_ET_MAGIC_NT_IO_CHANNEL);
|
|
|
|
|
|
|
|
if (channel->block_size != blksize) {
|
|
|
|
channel->block_size = blksize;
|
|
|
|
|
|
|
|
free(nt_data->buffer);
|
|
|
|
nt_data->buffer_block_number = 0xffffffff;
|
|
|
|
nt_data->buffer_size = channel->block_size;
|
2019-04-13 13:52:29 +00:00
|
|
|
assert((nt_data->buffer_size % 512) == 0);
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
nt_data->buffer = malloc(nt_data->buffer_size);
|
|
|
|
if (nt_data->buffer == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
[extfs] fix inodes not being initialized above 4 GB
* So, as it happens, when assigning the product of two 32-bit variables into a 64-bit one,
compilers default to being *DUMB* and, against all reasonable expectations, do not perform
that multiplication as a 64-bit operation (even when the code is compiled as x64). Wow,
that's really great decision making by compiler designers if I ever saw some... Whoever
decided that C developers would much rather want truncation and 32-bit overflows, instead
of the expected *LOGICAL* behaviour of conducting arithmetic operations as 64-bit when the
result will be assigned to a 64-bit variable, need to be condemned to a lifetime of trying
to help elderly folks trying to conduct simple computing tasks as a punishment...
Anyhoo, nt_write_blk()'s offset.QuadPart = block * channel->block_size + nt_data->offset
was overflowing 32-bit as soon as block * channel->block_size went over the 4 GB mark,
with the disastrous results one can expect. Considering that this is code we practically
lifted verbatim from e2fsprogs, I guess e2fsprogs' NT I/O manager was never properly
tested with anything larger than a 4 GB. Awesome!
* We fix the above by doing what unix_io.c does and setting the 32-bit read/write_blk()
calls to be wrappers around their 64-bit counterpart (since, once you deal with a 64-bit
block variable, the computation is conducted as 64-bit).
* Also remove a bunch of stuff we don't need from config.h
* Closes #1396
2020-02-12 16:26:22 +00:00
|
|
|
static errcode_t nt_read_blk64(io_channel channel, unsigned long long block, int count, void *buf)
|
2019-04-12 20:32:40 +00:00
|
|
|
{
|
|
|
|
PVOID read_buffer;
|
|
|
|
ULONG read_size;
|
|
|
|
ULONG size;
|
|
|
|
LARGE_INTEGER offset;
|
|
|
|
PNT_PRIVATE_DATA nt_data = NULL;
|
2019-05-02 14:41:42 +00:00
|
|
|
errcode_t errcode = 0;
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
|
|
|
|
nt_data = (PNT_PRIVATE_DATA) channel->private_data;
|
|
|
|
EXT2_CHECK_MAGIC(nt_data, EXT2_ET_MAGIC_NT_IO_CHANNEL);
|
|
|
|
|
|
|
|
// If it's in the cache, use it!
|
|
|
|
if ((count == 1) && (block == nt_data->buffer_block_number) &&
|
|
|
|
(nt_data->buffer_block_number != 0xffffffff)) {
|
|
|
|
memcpy(buf, nt_data->buffer, channel->block_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = (count < 0) ? (ULONG)(-count) : (ULONG)(count * channel->block_size);
|
|
|
|
|
2019-09-14 22:33:46 +00:00
|
|
|
offset.QuadPart = block * channel->block_size + nt_data->offset;
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
// If not fit to the block
|
|
|
|
if (size <= nt_data->buffer_size) {
|
|
|
|
// Update the cache
|
|
|
|
nt_data->buffer_block_number = block;
|
|
|
|
read_buffer = nt_data->buffer;
|
|
|
|
read_size = nt_data->buffer_size;
|
|
|
|
} else {
|
|
|
|
read_size = size;
|
|
|
|
read_buffer = buf;
|
2019-04-13 13:52:29 +00:00
|
|
|
assert((read_size % channel->block_size) == 0);
|
2019-04-12 20:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_RawRead(nt_data->handle, offset, read_size, read_buffer, &errcode)) {
|
|
|
|
if (channel->read_error)
|
|
|
|
return (channel->read_error)(channel, block, count, buf, size, 0, errcode);
|
|
|
|
else
|
|
|
|
return errcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_buffer != buf) {
|
2019-04-13 13:52:29 +00:00
|
|
|
assert(size <= read_size);
|
2019-04-12 20:32:40 +00:00
|
|
|
memcpy(buf, read_buffer, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
[extfs] fix inodes not being initialized above 4 GB
* So, as it happens, when assigning the product of two 32-bit variables into a 64-bit one,
compilers default to being *DUMB* and, against all reasonable expectations, do not perform
that multiplication as a 64-bit operation (even when the code is compiled as x64). Wow,
that's really great decision making by compiler designers if I ever saw some... Whoever
decided that C developers would much rather want truncation and 32-bit overflows, instead
of the expected *LOGICAL* behaviour of conducting arithmetic operations as 64-bit when the
result will be assigned to a 64-bit variable, need to be condemned to a lifetime of trying
to help elderly folks trying to conduct simple computing tasks as a punishment...
Anyhoo, nt_write_blk()'s offset.QuadPart = block * channel->block_size + nt_data->offset
was overflowing 32-bit as soon as block * channel->block_size went over the 4 GB mark,
with the disastrous results one can expect. Considering that this is code we practically
lifted verbatim from e2fsprogs, I guess e2fsprogs' NT I/O manager was never properly
tested with anything larger than a 4 GB. Awesome!
* We fix the above by doing what unix_io.c does and setting the 32-bit read/write_blk()
calls to be wrappers around their 64-bit counterpart (since, once you deal with a 64-bit
block variable, the computation is conducted as 64-bit).
* Also remove a bunch of stuff we don't need from config.h
* Closes #1396
2020-02-12 16:26:22 +00:00
|
|
|
static errcode_t nt_read_blk(io_channel channel, unsigned long block, int count, void* buf)
|
|
|
|
{
|
|
|
|
return nt_read_blk64(channel, block, count, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static errcode_t nt_write_blk64(io_channel channel, unsigned long long block, int count, const void *buf)
|
2019-04-12 20:32:40 +00:00
|
|
|
{
|
|
|
|
ULONG write_size;
|
|
|
|
LARGE_INTEGER offset;
|
|
|
|
PNT_PRIVATE_DATA nt_data = NULL;
|
2019-05-02 14:41:42 +00:00
|
|
|
errcode_t errcode = 0;
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
|
|
|
|
nt_data = (PNT_PRIVATE_DATA) channel->private_data;
|
|
|
|
EXT2_CHECK_MAGIC(nt_data, EXT2_ET_MAGIC_NT_IO_CHANNEL);
|
|
|
|
|
|
|
|
if (nt_data->read_only)
|
|
|
|
return EACCES;
|
|
|
|
|
|
|
|
if (count == 1) {
|
|
|
|
write_size = channel->block_size;
|
|
|
|
} else {
|
|
|
|
nt_data->buffer_block_number = 0xffffffff;
|
|
|
|
if (count < 0)
|
|
|
|
write_size = (ULONG)(-count);
|
|
|
|
else
|
|
|
|
write_size = (ULONG)(count * channel->block_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-13 13:52:29 +00:00
|
|
|
assert((write_size % 512) == 0);
|
2019-09-14 22:33:46 +00:00
|
|
|
offset.QuadPart = block * channel->block_size + nt_data->offset;
|
2019-04-12 20:32:40 +00:00
|
|
|
|
|
|
|
if (!_RawWrite(nt_data->handle, offset, write_size, buf, &errcode)) {
|
|
|
|
if (channel->write_error)
|
|
|
|
return (channel->write_error)(channel, block, count, buf, write_size, 0, errcode);
|
|
|
|
else
|
|
|
|
return errcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Stash a copy.
|
|
|
|
if(write_size >= nt_data->buffer_size) {
|
|
|
|
nt_data->buffer_block_number = block;
|
|
|
|
memcpy(nt_data->buffer, buf, nt_data->buffer_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
nt_data->written = TRUE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
[extfs] fix inodes not being initialized above 4 GB
* So, as it happens, when assigning the product of two 32-bit variables into a 64-bit one,
compilers default to being *DUMB* and, against all reasonable expectations, do not perform
that multiplication as a 64-bit operation (even when the code is compiled as x64). Wow,
that's really great decision making by compiler designers if I ever saw some... Whoever
decided that C developers would much rather want truncation and 32-bit overflows, instead
of the expected *LOGICAL* behaviour of conducting arithmetic operations as 64-bit when the
result will be assigned to a 64-bit variable, need to be condemned to a lifetime of trying
to help elderly folks trying to conduct simple computing tasks as a punishment...
Anyhoo, nt_write_blk()'s offset.QuadPart = block * channel->block_size + nt_data->offset
was overflowing 32-bit as soon as block * channel->block_size went over the 4 GB mark,
with the disastrous results one can expect. Considering that this is code we practically
lifted verbatim from e2fsprogs, I guess e2fsprogs' NT I/O manager was never properly
tested with anything larger than a 4 GB. Awesome!
* We fix the above by doing what unix_io.c does and setting the 32-bit read/write_blk()
calls to be wrappers around their 64-bit counterpart (since, once you deal with a 64-bit
block variable, the computation is conducted as 64-bit).
* Also remove a bunch of stuff we don't need from config.h
* Closes #1396
2020-02-12 16:26:22 +00:00
|
|
|
static errcode_t nt_write_blk(io_channel channel, unsigned long block, int count, const void* buf)
|
|
|
|
{
|
|
|
|
return nt_write_blk64(channel, block, count, buf);
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:32:40 +00:00
|
|
|
static errcode_t nt_flush(io_channel channel)
|
|
|
|
{
|
|
|
|
PNT_PRIVATE_DATA nt_data = NULL;
|
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL);
|
|
|
|
nt_data = (PNT_PRIVATE_DATA) channel->private_data;
|
|
|
|
EXT2_CHECK_MAGIC(nt_data, EXT2_ET_MAGIC_NT_IO_CHANNEL);
|
|
|
|
|
|
|
|
if(nt_data->read_only)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
// Flush file buffers.
|
|
|
|
_FlushDrive(nt_data->handle);
|
|
|
|
|
|
|
|
|
|
|
|
// Test and correct partition type.
|
|
|
|
if (nt_data->written)
|
|
|
|
_SetPartType(nt_data->handle, 0x83);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|