123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- /*++
- 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:
- partio.c
- Abstract:
- This module implements support for doing I/O directly to a partition in the
- setup application.
- Author:
- Evan Green 11-Apr-2014
- Environment:
- User
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <assert.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <getopt.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include "setup.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- PVOID
- SetupPartitionOpen (
- PSETUP_CONTEXT Context,
- PSETUP_DESTINATION Destination,
- PPARTITION_DEVICE_INFORMATION PartitionInformation
- )
- /*++
- Routine Description:
- This routine opens a handle to a given partition destination.
- Arguments:
- Context - Supplies a pointer to the application context.
- Destination - Supplies a pointer to the destination to open.
- PartitionInformation - Supplies an optional pointer where the partition
- information will be returned on success.
- Return Value:
- Returns a pointer to an opaque context on success.
- NULL on failure.
- --*/
- {
- PVOID Handle;
- INT Result;
- Handle = SetupOpenDestination(Destination, O_RDWR, 0);
- if (Handle != NULL) {
- SetupPartitionSeek(Context, Handle, 0);
- if (PartitionInformation != NULL) {
- Result = SetupOsGetPartitionInformation(Destination,
- PartitionInformation);
- if (Result != 0) {
- SetupPartitionClose(Context, Handle);
- Handle = NULL;
- }
- }
- }
- return Handle;
- }
- VOID
- SetupPartitionClose (
- PSETUP_CONTEXT Context,
- PVOID Handle
- )
- /*++
- Routine Description:
- This routine closes a partition.
- Arguments:
- Context - Supplies a pointer to the application context.
- Handle - Supplies the open handle.
- Return Value:
- None.
- --*/
- {
- SetupClose(Handle);
- return;
- }
- ssize_t
- SetupPartitionRead (
- PSETUP_CONTEXT Context,
- PVOID Handle,
- void *Buffer,
- size_t ByteCount
- )
- /*++
- Routine Description:
- This routine reads from a partition.
- Arguments:
- Context - Supplies a pointer to the application context.
- Handle - Supplies the handle.
- Buffer - Supplies a pointer where the read bytes will be returned.
- ByteCount - Supplies the number of bytes to read.
- Return Value:
- Returns the number of bytes read on success.
- --*/
- {
- return SetupRead(Handle, Buffer, ByteCount);
- }
- ssize_t
- SetupPartitionWrite (
- PSETUP_CONTEXT Context,
- PVOID Handle,
- void *Buffer,
- size_t ByteCount
- )
- /*++
- Routine Description:
- This routine writes to a partition.
- Arguments:
- Context - Supplies a pointer to the application context.
- Handle - Supplies the handle.
- Buffer - Supplies a pointer to the data to write.
- ByteCount - Supplies the number of bytes to read.
- Return Value:
- Returns the number of bytes written.
- --*/
- {
- return SetupWrite(Handle, Buffer, ByteCount);
- }
- LONGLONG
- SetupPartitionSeek (
- PSETUP_CONTEXT Context,
- PVOID Handle,
- LONGLONG Offset
- )
- /*++
- Routine Description:
- This routine seeks in the current file or device.
- Arguments:
- Context - Supplies a pointer to the application context.
- Handle - Supplies the handle.
- Offset - Supplies the offset in blocks to seek to.
- Return Value:
- Returns the resulting file offset in blocks after the operation.
- -1 on failure, and errno will contain more information. The file offset
- will remain unchanged.
- --*/
- {
- LONGLONG NewOffset;
- NewOffset = SetupSeek(
- Handle,
- (Offset + Context->CurrentPartitionOffset) * SETUP_BLOCK_SIZE);
- NewOffset -= Context->CurrentPartitionOffset * SETUP_BLOCK_SIZE;
- return NewOffset / SETUP_BLOCK_SIZE;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|