-
Notifications
You must be signed in to change notification settings - Fork 25
/
iin_optical.c
287 lines (260 loc) · 8.68 KB
/
iin_optical.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* iin_optical.c
* $Id: iin_optical.c,v 1.13 2007-05-12 20:17:08 bobi Exp $
*
* Copyright 2004 Bobi B., w1zard0f07@yahoo.com
*
* This file is part of hdl_dump.
*
* hdl_dump is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* hdl_dump is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with hdl_dump; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include "iin_optical.h"
#include "osal.h"
#include "retcodes.h"
#include "aligned.h"
typedef struct iin_optical_type
{
iin_t iin;
osal_handle_t device;
#if defined(IIN_OPTICAL_MMAP)
#define MMAP_AREA_SIZE IIN_SECTOR_SIZE * 65536 /* 128MB */
osal_mmap_t *mm;
u_int64_t total_size;
u_int64_t ptr_start, ptr_end;
void *ptr;
#else
aligned_t *al;
#endif
unsigned long error_code; /* against osal_... */
} iin_optical_t;
/**************************************************************/
static int
opt_stat(iin_t *iin,
/*@out@*/ u_int32_t *sector_size,
/*@out@*/ u_int32_t *num_sectors)
{
iin_optical_t *opt = (iin_optical_t *)iin;
u_int64_t size_in_bytes;
int result = osal_get_device_size(opt->device, &size_in_bytes);
if (result == OSAL_OK) {
*sector_size = IIN_SECTOR_SIZE;
*num_sectors = (u_int32_t)(size_in_bytes / IIN_SECTOR_SIZE);
} else
opt->error_code = osal_get_last_error_code();
return (result);
}
/**************************************************************/
static int
opt_read(iin_t *iin,
u_int32_t start_sector,
u_int32_t num_sectors,
/*@out@*/ const char **data,
/*@out@*/ u_int32_t *length)
{
iin_optical_t *opt = (iin_optical_t *)iin;
#if defined(IIN_OPTICAL_MMAP)
u_int64_t range_start = (u_int64_t)start_sector * IIN_SECTOR_SIZE;
u_int64_t range_end = range_start + num_sectors * IIN_SECTOR_SIZE;
if (range_end > opt->total_size)
range_end = opt->total_size;
if (!(opt->ptr_start <= range_start &&
range_end <= opt->ptr_end)) { /* need to (re)map requested region */
u_int64_t tmp_start = range_start;
u_int64_t tmp_end = range_start + MMAP_AREA_SIZE;
int result;
if (tmp_end > opt->total_size)
tmp_end = opt->total_size;
if (opt->ptr != NULL) { /* reset current mapping first */
result = osal_munmap(opt->mm);
if (result != OSAL_OK)
return (result);
opt->mm = NULL;
opt->ptr = NULL;
opt->ptr_start = opt->ptr_end = 0;
}
result = osal_mmap(&opt->mm, &opt->ptr, opt->device,
tmp_start, tmp_end - tmp_start);
if (result == OSAL_OK) { /* output args are set-up below */
opt->ptr_start = tmp_start;
opt->ptr_end = tmp_end;
} else
return (result);
}
/* information we need is memory-mapped already */
*data = (const char *)opt->ptr + (range_start - opt->ptr_start);
*length = (u_int32_t)(range_end - range_start);
return (RET_OK);
#else
int result = al_read(opt->al, (u_int64_t)start_sector * IIN_SECTOR_SIZE,
data, num_sectors * IIN_SECTOR_SIZE, length);
if (result == RET_OK)
;
else
opt->error_code = osal_get_last_error_code();
return (result);
#endif /* IIN_OPTICAL_MMAP defined? */
}
/**************************************************************/
static int
opt_close(/*@special@*/ /*@only@*/ iin_t *iin) /*@releases iin@*/
{
iin_optical_t *opt = (iin_optical_t *)iin;
int result;
#if defined(IIN_OPTICAL_MMAP)
if (opt->mm != NULL)
osal_munmap(opt->mm);
#else
al_free(opt->al);
#endif
result = osal_close(&opt->device);
if (result == RET_OK)
;
else
opt->error_code = osal_get_last_error_code();
osal_free(iin);
return (result);
}
/**************************************************************/
static char *
opt_last_error(iin_t *iin)
{
iin_optical_t *opt = (iin_optical_t *)iin;
return (osal_get_error_msg(opt->error_code));
}
/**************************************************************/
static void
opt_dispose_error(/*@unused@*/ iin_t *iin,
/*@only@*/ char *error)
{
osal_dispose_error_msg(error);
}
/**************************************************************/
static iin_optical_t *
opt_alloc(osal_handle_t device,
u_int32_t device_sector_size)
{
iin_optical_t *opt = (iin_optical_t *)osal_alloc(sizeof(iin_optical_t));
if (opt != NULL) {
iin_t *iin = &opt->iin;
#if !defined(IIN_OPTICAL_MMAP)
aligned_t *al = al_alloc(device, device_sector_size, IIN_SECTOR_SIZE * IIN_NUM_SECTORS / device_sector_size);
if (al != NULL) { /* success */
#endif
memset(opt, 0, sizeof(iin_optical_t));
iin->stat = &opt_stat;
iin->read = &opt_read;
iin->close = &opt_close;
iin->last_error = &opt_last_error;
iin->dispose_error = &opt_dispose_error;
strcpy(iin->source_type, "Optical drive");
opt->device = device;
#if defined(IIN_OPTICAL_MMAP)
opt->mm = NULL;
opt->ptr = NULL;
opt->ptr_start = opt->ptr_end = 0;
{
u_int32_t sectors, size;
if (opt_stat(iin, &size, §ors) == RET_OK)
opt->total_size = (u_int64_t)sectors * size;
else {
osal_free(opt);
opt = NULL;
}
}
#else
opt->al = al;
} else { /* failed */
osal_free(opt);
opt = NULL;
}
#endif
}
return (opt);
}
/**************************************************************/
int
iin_optical_probe_path(const char *path,
iin_t **iin)
{
if (tolower(path[0]) == 'c' &&
tolower(path[1]) == 'd' &&
isdigit(path[2]) &&
((path[3] == ':' &&
path[4] == '\0') ||
(isdigit(path[3]) &&
path[4] == ':' &&
path[5] == '\0'))) { /* "cd?:" or "cd??:" matched */
char device_name[MAX_PATH];
int result = osal_map_device_name(path, device_name);
if (result == OSAL_OK) {
osal_handle_t device;
result = osal_open(device_name, &device, 1);
if (result == OSAL_OK) {
u_int32_t sector_size;
result = osal_get_device_sect_size(device, §or_size);
if (result == OSAL_OK) {
*iin = (iin_t *)opt_alloc(device, sector_size);
if (*iin != NULL)
; /* success */
else { /* opt_alloc failed */
osal_close(&device);
result = RET_NO_MEM;
}
}
}
}
return (result);
}
else {
#if defined(_BUILD_WIN32)
return (RET_NOT_COMPAT);
#else
/* don't treat files as devices */
return (RET_NOT_COMPAT);
#if 0
/* FreeBSD patch to support device nodes */
char device_name [MAX_PATH];
int result = osal_map_device_name (path, device_name);
if (result == OSAL_OK)
{
osal_handle_t device;
result = osal_open (device_name, &device, 1);
if (result == OSAL_OK)
{
u_int32_t sector_size;
result = osal_get_device_sect_size (device, §or_size);
if (result == OSAL_OK)
{
*iin = (iin_t*) opt_alloc (device, sector_size);
if (*iin != NULL)
; /* success */
else
{ /* opt_alloc failed */
osal_close (&device);
result = RET_NO_MEM;
}
}
}
}
else
result = RET_NOT_COMPAT;
return (result);
#endif
#endif
}
}