4 * low-level 'block device' for input to cdrom discs.
6 * Copyright (c) 2009 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #ifndef cdrom_sector_H
20 #define cdrom_sector_H 1
23 #include <glib/gtypes.h>
24 #include "drivers/cdrom/defs.h"
30 /******************************** Sector Source ******************************/
31 #define SECTOR_SOURCE_TAG 0x42444556
32 typedef struct sector_source *sector_source_t;
39 } sector_source_type_t;
41 typedef cdrom_error_t (*sector_source_read_fn_t)(sector_source_t, cdrom_lba_t, cdrom_count_t, unsigned char *outbuf);
42 typedef cdrom_error_t (*sector_source_read_sectors_fn_t)(sector_source_t, cdrom_lba_t, cdrom_count_t, cdrom_read_mode_t mode,
43 unsigned char *outbuf, size_t *length);
44 typedef void (*sector_source_destroy_fn_t)(sector_source_t);
47 * A 'sector source' is a read-only random-access data source that supports
48 * reads of arbitrary blocks within their capacity. A block device has a
49 * defined mode, block size, and block count.
51 * Source are ref-counted, and automatically destroyed when the reference
54 struct sector_source {
55 uint32_t tag; /* sector source tag */
56 uint32_t ref_count; /* Reference count. Initialized to 0 */
57 sector_source_type_t type;
59 sector_mode_t mode; /* Implies sector size. */
60 cdrom_count_t size; /* Block count */
63 * Read blocks from the device using the native block size.
64 * @param buf Buffer to receive the blocks
65 * @param block First block to transfer (numbered from 0)
66 * @param block_count number of blocks to transfer.
67 * @return 0 on success, otherwise an error code.
69 sector_source_read_fn_t read_blocks;
72 * Read sectors from the device using the specified read mode, performing any
73 * necessary conversions.
75 sector_source_read_sectors_fn_t read_sectors;
78 * Release all resources and memory used by the device (note should never
81 sector_source_destroy_fn_t destroy;
86 * Block device that always returns zeros.
88 sector_source_t null_sector_source_new( sector_mode_t mode, cdrom_count_t size );
90 #define FILE_SECTOR_FULL_FILE ((cdrom_count_t)-1)
93 * File reader. Last block is 0-padded.
95 sector_source_t file_sector_source_new_filename( const gchar *filename, sector_mode_t mode,
96 uint32_t offset, cdrom_count_t sector_count );
97 sector_source_t file_sector_source_new( FILE *f, sector_mode_t mode, uint32_t offset, cdrom_count_t sector_count,
98 gboolean closeOnDestroy );
99 sector_source_t file_sector_source_new_full( FILE *f, sector_mode_t mode, gboolean closeOnDestroy );
102 * Temp-file creator - initially empty. Creates a file in the system temp dir,
103 * unlinked on destruction or program exit.
105 sector_source_t tmpfile_sector_source_new( sector_mode_t mode );
108 * Construct a file source that shares its file descriptor with another
111 sector_source_t file_sector_source_new_source( sector_source_t ref, sector_mode_t mode, uint32_t offset,
112 cdrom_count_t sector_count );
115 * Change the value of the source's closeOnDestroy flag
117 void file_sector_source_set_close_on_destroy( sector_source_t ref, gboolean closeOnDestroy );
120 * Retrieve the source's underlying FILE
122 FILE *file_sector_source_get_file( sector_source_t ref );
125 * Retrieve the source's underlying file descriptor
127 int file_sector_source_get_fd( sector_source_t ref );
129 /** Construct a memory source with the given mode and size */
130 sector_source_t mem_sector_source_new( sector_mode_t mode, cdrom_count_t size );
133 * Construct a memory source using the supplied buffer for data.
134 * @param buffer The buffer to read from, which must be at least size * sector_size in length
135 * @param mode The sector mode of the data in the buffer, which cannot be SECTOR_UNKNOWN
136 * @param size Number of sectors in the buffer
137 * @param freeOnDestroy If true, the source owns the buffer and will release it when the
138 * source is destroyed.
140 sector_source_t mem_sector_source_new_buffer( unsigned char *buffer, sector_mode_t mode, cdrom_count_t size,
141 gboolean freeOnDestroy );
144 * Retrieve the underlying buffer for a memory source
146 unsigned char *mem_sector_source_get_buffer( sector_source_t source );
149 * Increment the reference count for a block device.
151 void sector_source_ref( sector_source_t device );
154 * Unreference a block device. If decremented to 0, the device will be
157 void sector_source_unref( sector_source_t device );
160 * Release an unbound block device. If the ref count is 0, the device is
161 * destroyed. Otherwise the function has no effect.
163 void sector_source_release( sector_source_t device );
165 cdrom_error_t sector_source_read( sector_source_t device, cdrom_lba_t lba, cdrom_count_t block_count, unsigned char *buf );
167 cdrom_error_t sector_source_read_sectors( sector_source_t device, cdrom_lba_t lba, cdrom_count_t block_count,
168 cdrom_read_mode_t mode, unsigned char *buf, size_t *length );
170 /***** Internals for sector source implementations *****/
173 * Initialize a new (pre-allocated) sector source
175 sector_source_t sector_source_init( sector_source_t device, sector_source_type_t type, sector_mode_t mode, cdrom_count_t size,
176 sector_source_read_fn_t readfn, sector_source_destroy_fn_t destroyfn );
179 * Default sector source destructor method
181 void default_sector_source_destroy( sector_source_t device );
185 * Extract the necessary fields from a single raw sector for the given read mode.
186 * @param raw_sector input raw 2352 byte sector
187 * @param mode sector mode and field specification flags
188 * @param buf output buffer for sector data
189 * @param length output length of sector written to buf
190 * @return CDROM_ERROR_OK on success, otherwise an appropriate error code.
192 cdrom_error_t sector_extract_from_raw( unsigned char *raw_sector, cdrom_read_mode_t mode, unsigned char *buf, size_t *length );
195 * Test if the given pointer is a valid sector source
197 #define IS_SECTOR_SOURCE(dev) ((dev) != NULL && ((sector_source_t)(dev))->tag == SECTOR_SOURCE_TAG)
200 * Test if the given pointer is a valid sector source of the given type
202 #define IS_SECTOR_SOURCE_TYPE(dev,id) (IS_SECTOR_SOURCE(dev) && ((sector_source_t)(dev))->type == id)
210 #endif /* !cdrom_sector_H */
.