-
Notifications
You must be signed in to change notification settings - Fork 25
/
osal_unix.c
413 lines (356 loc) · 11.8 KB
/
osal_unix.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* osal_unix.c
* $Id: osal_unix.c,v 1.9 2007-05-12 20:17:38 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 <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
/* patch for MacOS X + external USB HDD box by G.S. */
#include <sys/disk.h>
#endif
#include <fcntl.h>
#include "retcodes.h"
#include "osal.h"
#include "apa.h"
/* memory-mapped files */
struct osal_mmap_type
{ /* keep anything required for unmap operation */
void *start;
size_t length;
};
/**************************************************************/
unsigned long
osal_get_last_error_code(void)
{
return (errno);
}
/**************************************************************/
char *
osal_get_error_msg(unsigned long err)
{
return (strerror(err));
}
/**************************************************************/
char *
osal_get_last_error_msg(void)
{
return (osal_get_error_msg(osal_get_last_error_code()));
}
/**************************************************************/
void osal_dispose_error_msg(char *msg)
{
/* this point should not be freed */
msg = NULL;
}
/**************************************************************/
int /* OSAL_OK, OSAL_ERR */
osal_open(const char *name,
osal_handle_t *handle,
int no_cache)
{
handle->desc = open64(name, O_RDONLY | O_LARGEFILE, 0);
return (handle->desc == -1 ? OSAL_ERR : OSAL_OK);
}
/**************************************************************/
int /* OSAL_OK, OSAL_ERR */
osal_open_device_for_writing(const char *device_name,
osal_handle_t *handle)
{
handle->desc = open(device_name, O_RDWR | O_LARGEFILE | O_DSYNC, S_IRUSR | S_IWUSR);
return (handle->desc == -1 ? OSAL_ERR : OSAL_OK);
}
/**************************************************************/
int /* OSAL_OK, OSAL_ERR */
osal_create_file(const char *path,
osal_handle_t *handle,
u_int64_t estimated_size)
{
int result = RET_ERR;
handle->desc = open64(path,
O_CREAT | O_EXCL | O_LARGEFILE | O_RDWR,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (handle->desc != -1) { /* success */
if (estimated_size > 0) {
off64_t offs = lseek64(handle->desc, estimated_size - 1, SEEK_END);
if (offs != -1) {
char dummy = '\0';
u_int32_t bytes = write(handle->desc, &dummy, 1);
if (bytes == 1) {
offs = lseek64(handle->desc, 0, SEEK_SET);
if (offs == 0) { /* success */
result = RET_OK;
}
}
}
} else
result = RET_OK;
if (result != RET_OK) { /* delete file on error */
close(handle->desc);
handle->desc = -1; /* make sure "is file opened" test would fail */
unlink(path);
}
} else
result = RET_ERR;
return (result);
}
/**************************************************************/
int /* OSAL_OK, OSAL_ERR */
osal_get_estimated_device_size(osal_handle_t handle,
u_int64_t *size_in_bytes)
{
struct stat64 st;
int result;
memset(&st, 0, sizeof(struct stat64)); /* play on the safe side */
result = fstat64(handle.desc, &st) == 0 ? RET_OK : RET_ERR;
if (result == RET_OK) { /* success */
*size_in_bytes = st.st_size; /* might be 0 for block devices? */
if (*size_in_bytes == 0) { /* try with lseek... */
off64_t curr = lseek64(handle.desc, 0, SEEK_CUR);
if (curr >= 0) {
off64_t size = lseek64(handle.desc, 0, SEEK_END);
#if defined(__APPLE__)
/* patch for MacOS X + external USB HDD box by G.S. */
if (size == 0) {
u_int32_t blocksize;
u_int64_t blockcount;
if (ioctl(handle.desc, DKIOCGETBLOCKSIZE, &blocksize) < 0)
return (OSAL_ERR);
if (ioctl(handle.desc, DKIOCGETBLOCKCOUNT, &blockcount) < 0)
return (OSAL_ERR);
size = blockcount * blocksize;
}
#elif defined(__FreeBSD__)
/* basically the same as MacOS X patch, but ioctl is different */
if (size == 0) {
u_int blocksize;
off_t mediasize;
if (ioctl(handle.desc, DIOCGSECTORSIZE, &blocksize) < 0)
return (OSAL_ERR);
if (ioctl(handle.desc, DIOCGMEDIASIZE, &mediasize) < 0)
return (OSAL_ERR);
size = mediasize;
}
#endif
if (size >= 0) {
*size_in_bytes = size;
result = (lseek64(handle.desc, curr, SEEK_SET) >= 0 ?
OSAL_OK :
OSAL_ERR);
} else
result = RET_ERR;
} else
result = RET_ERR;
}
} else
result = RET_ERR;
return (result);
}
/**************************************************************/
int /* OSAL_OK, OSAL_ERR */
osal_get_device_size(osal_handle_t handle,
u_int64_t *size_in_bytes)
{
return (osal_get_estimated_device_size(handle, size_in_bytes));
}
/**************************************************************/
int osal_get_device_sect_size(osal_handle_t handle,
u_int32_t *size_in_bytes)
{ /* TODO: osal_get_device_sect_size */
*size_in_bytes = 4096; /* that is a resonable sector size */
return (OSAL_OK);
}
/**************************************************************/
int osal_get_volume_sect_size(const char *volume_root,
u_int32_t *size_in_bytes)
{
struct stat64 st;
int result = stat64(volume_root, &st) == 0 ? RET_OK : RET_ERR;
if (result == RET_OK)
*size_in_bytes = st.st_blksize;
return (result);
}
/**************************************************************/
int osal_get_file_size(osal_handle_t handle,
u_int64_t *size_in_bytes)
{
off64_t offs_curr = lseek64(handle.desc, 0, SEEK_CUR);
if (offs_curr != -1) {
off64_t offs_end = lseek64(handle.desc, 0, SEEK_END);
if (offs_end != -1) {
if (lseek64(handle.desc, offs_curr, SEEK_SET) == offs_curr) { /* success */
*size_in_bytes = offs_end;
return (RET_OK);
}
}
}
return (RET_ERR);
}
/**************************************************************/
int osal_get_file_size_ex(const char *path,
u_int64_t *size_in_bytes)
{
osal_handle_t in;
int result = osal_open(path, &in, 1);
if (result == OSAL_OK) {
result = osal_get_file_size(in, size_in_bytes);
osal_close(&in);
}
return (result);
}
/**************************************************************/
int osal_seek(osal_handle_t handle,
u_int64_t abs_pos)
{
return (lseek64(handle.desc, abs_pos, SEEK_SET) == -1 ? OSAL_ERR : OSAL_OK);
}
/**************************************************************/
int /* OSAL_OK, OSAL_ERR */
osal_read(osal_handle_t handle,
void *out,
u_int32_t bytes,
u_int32_t *stored)
{
int n = read(handle.desc, out, bytes);
if (n != -1) { /* success */
*stored = n;
return (OSAL_OK);
} else
return (OSAL_ERR);
}
/**************************************************************/
int /* OSAL_OK, OSAL_ERR */
osal_write(osal_handle_t handle,
const void *in,
u_int32_t bytes,
u_int32_t *stored)
{
int n = write(handle.desc, in, bytes);
if (n != -1) { /* success */
*stored = n;
return (OSAL_OK);
} else
return (OSAL_ERR);
}
/**************************************************************/
int /* OSAL_OK, OSAL_ERR */
osal_close(osal_handle_t *handle)
{
return (close(handle->desc) == -1 ? OSAL_ERR : OSAL_OK);
}
/**************************************************************/
void *
osal_alloc(u_int32_t bytes)
{
return (malloc(bytes));
}
/**************************************************************/
void osal_free(void *ptr)
{
if (ptr != NULL)
free(ptr);
}
/**************************************************************/
int osal_mmap(osal_mmap_t **mm,
void **p,
osal_handle_t handle,
u_int64_t offset,
u_int32_t length)
{
*mm = osal_alloc(sizeof(osal_mmap_t));
if (*mm != NULL) {
int align = offset % getpagesize();
void *addr;
offset -= align;
length += align;
addr = mmap64(NULL, length, PROT_READ, MAP_SHARED,
handle.desc, offset);
if (addr != MAP_FAILED) { /* success */
(*mm)->start = addr;
(*mm)->length = length;
*p = (u_int8_t *)addr + align;
return (OSAL_OK);
} else { /* mmap failed */
osal_free(*mm), *mm = NULL;
return (OSAL_ERR);
}
} else /* malloc failed */
return (OSAL_NO_MEM);
}
/**************************************************************/
int osal_munmap(osal_mmap_t *mm)
{
int result = munmap(mm->start, mm->length);
if (result == 0)
osal_free(mm);
return (result == 0 ? OSAL_OK : OSAL_ERR);
}
/**************************************************************/
int osal_query_hard_drives(osal_dlist_t **hard_drives)
{
/* TODO: under unix, they are like files */
return (OSAL_ERR);
}
/**************************************************************/
int osal_query_optical_drives(osal_dlist_t **optical_drives)
{
/* TODO: under unix, they are like files */
return (OSAL_ERR);
}
/**************************************************************/
int osal_query_devices(osal_dlist_t **hard_drives,
osal_dlist_t **optical_drives)
{
int result = osal_query_hard_drives(hard_drives);
if (result == RET_OK)
result = osal_query_optical_drives(optical_drives);
return (result);
}
/**************************************************************/
void osal_dlist_free(osal_dlist_t *dlist)
{
if (dlist != NULL) {
if (dlist->device != NULL)
osal_free(dlist->device);
osal_free(dlist);
}
}
/**************************************************************/
int /* RET_OK, RET_BAD_FORMAT, RET_BAD_DEVICE */
osal_map_device_name(const char *input,
char output[MAX_PATH])
{
struct stat64 st;
int result = stat64(input, &st) == 0 ? RET_OK : RET_ERR;
if (result == RET_OK) { /* accept the input, only if it is a block device */
#if !defined(_DEBUG) /* in debug mode treat files as devices */
result = st.st_mode & S_IFBLK ? RET_OK : RET_BAD_DEVICE;
#endif
if (result == RET_OK)
strcpy(output, input);
}
return (result);
}