123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982 |
- /*++
- Copyright (c) 2014 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- fatdev.c
- Abstract:
- This module implements support for FAT disk I/O in the setup packet.
- Author:
- Evan Green 14-Apr-2014
- Environment:
- User
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <assert.h>
- #include <errno.h>
- #include <getopt.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <time.h>
- #include "setup.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- typedef VOID FAT_IO_BUFFER, *PFAT_IO_BUFFER;
- /*++
- Structure Description:
- This structure defines an I/O buffer for the setup program.
- Members:
- Data - Stores a pointer to the I/O buffer's data buffer.
- Size - Stores the size of the memory buffer, in bytes.
- CurrentOffset - Stores the current offset into the I/O buffer. All I/O will
- begin at the current offset.
- --*/
- typedef struct _SETUP_FAT_IO_BUFFER {
- PVOID Data;
- UINTN Size;
- UINTN CurrentOffset;
- } SETUP_FAT_IO_BUFFER, *PSETUP_FAT_IO_BUFFER;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- PFAT_IO_BUFFER
- FatAllocateIoBuffer (
- PVOID DeviceToken,
- UINTN Size
- )
- /*++
- Routine Description:
- This routine allocates memory for device I/O use.
- Arguments:
- DeviceToken - Supplies an opaque token identifying the underlying device.
- Size - Supplies the size of the required allocation, in bytes.
- Return Value:
- Returns a pointer to the FAT I/O buffer, or NULL on failure.
- --*/
- {
- PSETUP_FAT_IO_BUFFER IoBuffer;
- IoBuffer = malloc(sizeof(SETUP_FAT_IO_BUFFER) + Size);
- if (IoBuffer == NULL) {
- return NULL;
- }
- IoBuffer->Data = (PVOID)IoBuffer + sizeof(SETUP_FAT_IO_BUFFER);
- IoBuffer->Size = Size;
- IoBuffer->CurrentOffset = 0;
- return IoBuffer;
- }
- PFAT_IO_BUFFER
- FatCreateIoBuffer (
- PVOID Buffer,
- UINTN Size
- )
- /*++
- Routine Description:
- This routine creates a FAT I/O buffer from the given buffer.
- Arguments:
- Buffer - Supplies a pointer to the memory buffer on which to base the FAT
- I/O buffer.
- Size - Supplies the size of the memory buffer, in bytes.
- Return Value:
- Returns an pointer to the FAT I/O buffer, or NULL on failure.
- --*/
- {
- PSETUP_FAT_IO_BUFFER IoBuffer;
- IoBuffer = malloc(sizeof(SETUP_FAT_IO_BUFFER));
- if (IoBuffer == NULL) {
- return NULL;
- }
- IoBuffer->Data = Buffer;
- IoBuffer->Size = Size;
- IoBuffer->CurrentOffset = 0;
- return IoBuffer;
- }
- VOID
- FatIoBufferUpdateOffset (
- PFAT_IO_BUFFER FatIoBuffer,
- UINTN OffsetUpdate,
- BOOL Decrement
- )
- /*++
- Routine Description:
- This routine increments the given FAT I/O buffer's current offset by the
- given amount.
- Arguments:
- FatIoBuffer - Supplies a pointer to a FAT I/O buffer.
- OffsetUpdate - Supplies the number of bytes by which the offset will be
- updated.
- Decrement - Supplies a boolean indicating whether the update will be a
- decrement (TRUE) or an increment (FALSE).
- Return Value:
- None.
- --*/
- {
- if (Decrement == FALSE) {
- ((PSETUP_FAT_IO_BUFFER)FatIoBuffer)->CurrentOffset += OffsetUpdate;
- } else {
- ((PSETUP_FAT_IO_BUFFER)FatIoBuffer)->CurrentOffset -= OffsetUpdate;
- }
- ASSERT(((PSETUP_FAT_IO_BUFFER)FatIoBuffer)->CurrentOffset <=
- ((PSETUP_FAT_IO_BUFFER)FatIoBuffer)->Size);
- return;
- }
- VOID
- FatIoBufferSetOffset (
- PFAT_IO_BUFFER FatIoBuffer,
- UINTN Offset
- )
- /*++
- Routine Description:
- This routine sets the given FAT I/O buffer's current offset.
- Arguments:
- FatIoBuffer - Supplies a pointer to a FAT I/O buffer.
- Offset - Supplies the new offset to set.
- Return Value:
- None.
- --*/
- {
- PSETUP_FAT_IO_BUFFER IoBuffer;
- IoBuffer = (PSETUP_FAT_IO_BUFFER)FatIoBuffer;
- IoBuffer->CurrentOffset = Offset;
- ASSERT(IoBuffer->CurrentOffset <= IoBuffer->Size);
- return;
- }
- KSTATUS
- FatZeroIoBuffer (
- PFAT_IO_BUFFER FatIoBuffer,
- UINTN Offset,
- UINTN ByteCount
- )
- /*++
- Routine Description:
- This routine zeros the contents of the FAT I/O buffer starting at the
- offset for the given number of bytes.
- Arguments:
- FatIoBuffer - Supplies a pointer to the FAT I/O buffer that is to be zeroed.
- Offset - Supplies the offset within the I/O buffer where the zeroing should
- begin.
- ByteCount - Supplies the number of bytes to zero.
- Return Value:
- Status code.
- --*/
- {
- PSETUP_FAT_IO_BUFFER IoBuffer;
- IoBuffer = (PSETUP_FAT_IO_BUFFER)FatIoBuffer;
- RtlZeroMemory(IoBuffer->Data + IoBuffer->CurrentOffset + Offset, ByteCount);
- return STATUS_SUCCESS;
- }
- KSTATUS
- FatCopyIoBuffer (
- PFAT_IO_BUFFER Destination,
- UINTN DestinationOffset,
- PFAT_IO_BUFFER Source,
- UINTN SourceOffset,
- UINTN ByteCount
- )
- /*++
- Routine Description:
- This routine copies the contents of the source I/O buffer starting at the
- source offset to the destination I/O buffer starting at the destination
- offset. It assumes that the arguments are correct such that the copy can
- succeed.
- Arguments:
- Destination - Supplies a pointer to the destination I/O buffer that is to
- be copied into.
- DestinationOffset - Supplies the offset into the destination I/O buffer
- where the copy should begin.
- Source - Supplies a pointer to the source I/O buffer whose contents will be
- copied to the destination.
- SourceOffset - Supplies the offset into the source I/O buffer where the
- copy should begin.
- ByteCount - Supplies the size of the requested copy in bytes.
- Return Value:
- Status code.
- --*/
- {
- PVOID DestinationBuffer;
- PSETUP_FAT_IO_BUFFER DestinationIoBuffer;
- PVOID SourceBuffer;
- PSETUP_FAT_IO_BUFFER SourceIoBuffer;
- DestinationIoBuffer = (PSETUP_FAT_IO_BUFFER)Destination;
- DestinationBuffer = DestinationIoBuffer->Data +
- DestinationIoBuffer->CurrentOffset +
- DestinationOffset;
- SourceIoBuffer = (PSETUP_FAT_IO_BUFFER)Source;
- SourceBuffer = SourceIoBuffer->Data +
- SourceIoBuffer->CurrentOffset +
- SourceOffset;
- RtlCopyMemory(DestinationBuffer, SourceBuffer, ByteCount);
- return STATUS_SUCCESS;
- }
- KSTATUS
- FatCopyIoBufferData (
- PFAT_IO_BUFFER FatIoBuffer,
- PVOID Buffer,
- UINTN Offset,
- UINTN Size,
- BOOL ToIoBuffer
- )
- /*++
- Routine Description:
- This routine copies from a buffer into the given I/O buffer or out of the
- given I/O buffer.
- Arguments:
- FatIoBuffer - Supplies a pointer to the FAT I/O buffer to copy in or out of.
- Buffer - Supplies a pointer to the regular linear buffer to copy to or from.
- Offset - Supplies an offset in bytes from the beginning of the I/O buffer
- to copy to or from.
- Size - Supplies the number of bytes to copy.
- ToIoBuffer - Supplies a boolean indicating whether data is copied into the
- I/O buffer (TRUE) or out of the I/O buffer (FALSE).
- Return Value:
- Status code.
- --*/
- {
- PVOID Destination;
- PSETUP_FAT_IO_BUFFER IoBuffer;
- PVOID Source;
- IoBuffer = (PSETUP_FAT_IO_BUFFER)FatIoBuffer;
- ASSERT(IoBuffer->CurrentOffset + Offset + Size <= IoBuffer->Size);
- Destination = IoBuffer->Data + IoBuffer->CurrentOffset + Offset;
- Source = Buffer;
- if (ToIoBuffer == FALSE) {
- Source = Destination;
- Destination = Buffer;
- }
- RtlCopyMemory(Destination, Source, Size);
- return STATUS_SUCCESS;
- }
- PVOID
- FatMapIoBuffer (
- PFAT_IO_BUFFER FatIoBuffer
- )
- /*++
- Routine Description:
- This routine maps the given FAT I/O buffer and returns the base of the
- virtually contiguous mapping.
- Arguments:
- FatIoBuffer - Supplies a pointer to a FAT I/O buffer.
- Return Value:
- Returns a pointer to the virtual address of the mapping on success, or
- NULL on failure.
- --*/
- {
- return ((PSETUP_FAT_IO_BUFFER)FatIoBuffer)->Data;
- }
- VOID
- FatFreeIoBuffer (
- PFAT_IO_BUFFER FatIoBuffer
- )
- /*++
- Routine Description:
- This routine frees a FAT I/O buffer.
- Arguments:
- FatIoBuffer - Supplies a pointer to a FAT I/O buffer.
- Return Value:
- None.
- --*/
- {
- free(FatIoBuffer);
- return;
- }
- PVOID
- FatAllocatePagedMemory (
- PVOID DeviceToken,
- ULONG SizeInBytes
- )
- /*++
- Routine Description:
- This routine allocates paged memory for the FAT library.
- Arguments:
- DeviceToken - Supplies an opaque token identifying the underlying device.
- SizeInBytes - Supplies the number of bytes to allocate.
- Return Value:
- Returns a pointer to the allocated memory, or NULL on failure.
- --*/
- {
- return malloc(SizeInBytes);
- }
- PVOID
- FatAllocateNonPagedMemory (
- PVOID DeviceToken,
- ULONG SizeInBytes
- )
- /*++
- Routine Description:
- This routine allocates non-paged memory for the FAT library.
- Arguments:
- DeviceToken - Supplies an opaque token identifying the underlying device.
- SizeInBytes - Supplies the number of bytes to allocate.
- Return Value:
- Returns a pointer to the allocated memory, or NULL on failure.
- --*/
- {
- return malloc(SizeInBytes);
- }
- VOID
- FatFreePagedMemory (
- PVOID DeviceToken,
- PVOID Allocation
- )
- /*++
- Routine Description:
- This routine frees paged memory for the FAT library.
- Arguments:
- DeviceToken - Supplies an opaque token identifying the underlying device.
- Allocation - Supplies a pointer to the allocation to free.
- Return Value:
- None.
- --*/
- {
- free(Allocation);
- return;
- }
- VOID
- FatFreeNonPagedMemory (
- PVOID DeviceToken,
- PVOID Allocation
- )
- /*++
- Routine Description:
- This routine frees memory for the FAT library.
- Arguments:
- DeviceToken - Supplies an opaque token identifying the underlying device.
- Allocation - Supplies a pointer to the allocation to free.
- Return Value:
- None.
- --*/
- {
- free(Allocation);
- return;
- }
- KSTATUS
- FatCreateLock (
- PVOID *Lock
- )
- /*++
- Routine Description:
- This routine creates a lock.
- Arguments:
- Lock - Supplies a pointer where an opaque pointer will be returned
- representing the lock.
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_INSUFFICIENT_RESOURCES if the lock could not be allocated.
- --*/
- {
- *Lock = NULL;
- return STATUS_SUCCESS;
- }
- VOID
- FatDestroyLock (
- PVOID Lock
- )
- /*++
- Routine Description:
- This routine destroys a created lock.
- Arguments:
- Lock - Supplies a the opaque pointer returned upon creation.
- Return Value:
- None.
- --*/
- {
- return;
- }
- VOID
- FatAcquireLock (
- PVOID Lock
- )
- /*++
- Routine Description:
- This routine acquires a lock.
- Arguments:
- Lock - Supplies a the opaque pointer returned upon creation.
- Return Value:
- None.
- --*/
- {
- return;
- }
- VOID
- FatReleaseLock (
- PVOID Lock
- )
- /*++
- Routine Description:
- This routine releases a lock.
- Arguments:
- Lock - Supplies a the opaque pointer returned upon creation.
- Return Value:
- None.
- --*/
- {
- return;
- }
- KSTATUS
- FatReadDevice (
- PVOID DeviceToken,
- ULONGLONG BlockAddress,
- UINTN BlockCount,
- ULONG Flags,
- PVOID Irp,
- PFAT_IO_BUFFER *FatIoBuffer
- )
- /*++
- Routine Description:
- This routine reads data from the underlying disk.
- Arguments:
- DeviceToken - Supplies an opaque token identifying the underlying device.
- BlockAddress - Supplies the block index to read (for physical disks, this is
- the LBA).
- BlockCount - Supplies the number of blocks to read.
- Flags - Supplies flags regarding the I/O operation. See IO_FLAG_*
- definitions.
- Irp - Supplies an optional pointer to the IRP to pass to the read file
- function.
- FatIoBuffer - Supplies a pointer to a FAT I/O buffer where the data from
- the disk will be returned.
- Return Value:
- Status code.
- --*/
- {
- PVOID Buffer;
- size_t ByteCount;
- ssize_t BytesRead;
- PSETUP_CONTEXT Context;
- PSETUP_FAT_IO_BUFFER IoBuffer;
- KSTATUS Status;
- PSETUP_VOLUME Volume;
- assert(SETUP_BLOCK_SIZE != 0);
- assert(FatIoBuffer != NULL);
- Volume = DeviceToken;
- Context = Volume->Context;
- IoBuffer = (PSETUP_FAT_IO_BUFFER)FatIoBuffer;
- Buffer = IoBuffer->Data + IoBuffer->CurrentOffset;
- ByteCount = BlockCount * SETUP_BLOCK_SIZE;
- assert((IoBuffer->Size - IoBuffer->CurrentOffset) >= ByteCount);
- if ((Volume->DestinationType == SetupDestinationPartition) ||
- (Volume->DestinationType == SetupDestinationDisk)) {
- SetupPartitionSeek(Context, Volume->BlockHandle, BlockAddress);
- BytesRead = SetupPartitionRead(Context,
- Volume->BlockHandle,
- Buffer,
- ByteCount);
- } else {
- SetupSeek(Volume->BlockHandle, BlockAddress * SETUP_BLOCK_SIZE);
- BytesRead = SetupRead(Volume->BlockHandle, Buffer, ByteCount);
- }
- if (BytesRead != ByteCount) {
- Status = STATUS_DEVICE_IO_ERROR;
- goto ReadDeviceEnd;
- }
- Status = STATUS_SUCCESS;
- ReadDeviceEnd:
- if (!KSUCCESS(Status)) {
- printf("Failed to read FAT device: %d\n", Status);
- }
- return Status;
- }
- KSTATUS
- FatWriteDevice (
- PVOID DeviceToken,
- ULONGLONG BlockAddress,
- UINTN BlockCount,
- ULONG Flags,
- PVOID Irp,
- PFAT_IO_BUFFER FatIoBuffer
- )
- /*++
- Routine Description:
- This routine writes data to the underlying disk.
- Arguments:
- DeviceToken - Supplies an opaque token identifying the underlying device.
- BlockAddress - Supplies the block index to write to (for physical disks,
- this is the LBA).
- BlockCount - Supplies the number of blocks to write.
- Flags - Supplies flags regarding the I/O operation. See IO_FLAG_*
- definitions.
- Irp - Supplies an optional pointer to an IRP to use for the disk operation.
- FatIoBuffer - Supplies a pointer to a FAT I/O buffer containing the data to
- write.
- Return Value:
- Status code.
- --*/
- {
- ULONG BlockSize;
- PVOID Buffer;
- ssize_t BytesWritten;
- PSETUP_CONTEXT Context;
- PSETUP_FAT_IO_BUFFER SetupIoBuffer;
- KSTATUS Status;
- PSETUP_VOLUME Volume;
- Volume = DeviceToken;
- Context = Volume->Context;
- BlockSize = SETUP_BLOCK_SIZE;
- assert(BlockSize != 0);
- SetupIoBuffer = (PSETUP_FAT_IO_BUFFER)FatIoBuffer;
- Buffer = SetupIoBuffer->Data + SetupIoBuffer->CurrentOffset;
- if ((Volume->DestinationType == SetupDestinationPartition) ||
- (Volume->DestinationType == SetupDestinationDisk)) {
- SetupPartitionSeek(Context, Volume->BlockHandle, BlockAddress);
- BytesWritten = SetupPartitionWrite(Context,
- Volume->BlockHandle,
- Buffer,
- BlockCount * BlockSize);
- } else {
- SetupSeek(Volume->BlockHandle, BlockAddress * BlockSize);
- BytesWritten = SetupWrite(Volume->BlockHandle,
- Buffer,
- BlockCount * BlockSize);
- }
- if (BytesWritten != BlockCount * BlockSize) {
- Status = STATUS_DEVICE_IO_ERROR;
- goto WriteDeviceEnd;
- }
- Status = STATUS_SUCCESS;
- WriteDeviceEnd:
- if (!KSUCCESS(Status)) {
- printf("Failed to write FAT device: %d\n", Status);
- }
- return Status;
- }
- KSTATUS
- FatGetDeviceBlockInformation (
- PVOID DeviceToken,
- PFILE_BLOCK_INFORMATION BlockInformation
- )
- /*++
- Routine Description:
- This routine converts a file's block information into disk level block
- information by modifying the offsets of each contiguous run.
- Arguments:
- DeviceToken - Supplies an opaque token identify the underlying device.
- BlockInformation - Supplies a pointer to the block information to be
- updated.
- Return Value:
- Status code.
- --*/
- {
- ASSERT(FALSE);
- return STATUS_NOT_IMPLEMENTED;
- }
- ULONG
- FatGetIoCacheEntryDataSize (
- VOID
- )
- /*++
- Routine Description:
- This routine returns the size of data stored in each cache entry.
- Arguments:
- None.
- Return Value:
- Returns the size of the data stored in each cache entry, or 0 if there is
- no cache.
- --*/
- {
- return 0;
- }
- ULONG
- FatGetPageSize (
- VOID
- )
- /*++
- Routine Description:
- This routine returns the size of a physical memory page for the current FAT
- environment.
- Arguments:
- None.
- Return Value:
- Returns the size of a page in the current environment. Returns 0 if the
- size is not known.
- --*/
- {
- return 0;
- }
- VOID
- FatGetCurrentSystemTime (
- PSYSTEM_TIME SystemTime
- )
- /*++
- Routine Description:
- This routine returns the current system time.
- Arguments:
- SystemTime - Supplies a pointer where the current system time will be
- returned.
- Return Value:
- None.
- --*/
- {
- SystemTime->Seconds = (LONGLONG)time(NULL) - SYSTEM_TIME_TO_EPOCH_DELTA;
- SystemTime->Nanoseconds = 0;
- return;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|