summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libc/libio.c
blob: 3a7325c4229cd68da17cee732b26fec456fcedbd (plain) (blame)
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 *  Provide UNIX/POSIX-like io system calls for RTEMS using the
 *  RTEMS IO manager
 *
 *  $Id$
 */

#include <rtems.h>
#include <rtems/assoc.h>                /* assoc.h not included by rtems.h */

#include <stdio.h>                      /* O_RDONLY, et.al. */
#include <fcntl.h>                      /* O_RDONLY, et.al. */
#include <assert.h>

#if ! defined(O_NDELAY)
# if defined(solaris2)
#  define O_NDELAY O_NONBLOCK
# elif defined(RTEMS_NEWLIB)
#  define O_NDELAY _FNBIO
# endif
#endif


#include <errno.h>
#include <string.h>                     /* strcmp */
#include <unistd.h>
#include <stdlib.h>                     /* calloc() */

#include "libio.h"                      /* libio.h not pulled in by rtems */

/*
 * Semaphore to protect the io table
 */

Objects_Id rtems_libio_semaphore;

#define RTEMS_LIBIO_SEM         rtems_build_name('L', 'B', 'I', 'O')
#define RTEMS_LIBIO_IOP_SEM(n)  rtems_build_name('L', 'B', 'I', n)

unsigned32     rtems_libio_number_iops;
rtems_libio_t *rtems_libio_iops;
rtems_libio_t *rtems_libio_last_iop;

#define rtems_libio_iop(fd)    ((((unsigned32)(fd)) < rtems_libio_number_iops) ? \
                                       &rtems_libio_iops[fd] : 0)

#define rtems_libio_check_fd(fd) \
    do { \
        if ((unsigned32) (fd) >= rtems_libio_number_iops) \
        { \
            errno = EBADF; \
            return -1; \
        } \
    } while (0)

#define rtems_libio_check_buffer(buffer) \
    do { \
        if ((buffer) == 0) \
        { \
            errno = EINVAL; \
            return -1; \
        } \
    } while (0)

#define rtems_libio_check_count(count) \
    do { \
        if ((count) == 0) \
        { \
            return 0; \
        } \
    } while (0)

#define rtems_libio_check_permissions(iop, flag) \
    do { \
        if (((iop)->flags & (flag)) == 0) \
        { \
              errno = EINVAL; \
              return -1; \
        } \
    } while (0)


void
rtems_libio_config(
    rtems_configuration_table *config,
    unsigned32                 max_fds
  )
{
    rtems_libio_number_iops = max_fds;

    /*
     * tweak config to reflect # of semaphores we will need
     */

    /* one for iop table */
    config->RTEMS_api_configuration->maximum_semaphores += 1; 
    config->RTEMS_api_configuration->maximum_semaphores += max_fds;
}

/*
 * Called by bsp startup code to init the libio area.
 */

void
rtems_libio_init(void)
{
    rtems_status_code rc;

    if (rtems_libio_number_iops > 0)
    {
        rtems_libio_iops = (rtems_libio_t *) calloc(rtems_libio_number_iops,
                                                    sizeof(rtems_libio_t));
        if (rtems_libio_iops == NULL)
            rtems_fatal_error_occurred(RTEMS_NO_MEMORY);

        rtems_libio_last_iop = rtems_libio_iops + (rtems_libio_number_iops - 1);
    }

    rc = rtems_semaphore_create(
      RTEMS_LIBIO_SEM,
      1,
      RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
      RTEMS_NO_PRIORITY,
      &rtems_libio_semaphore
    );
    if (rc != RTEMS_SUCCESSFUL)
        rtems_fatal_error_occurred(rc);
}

/*
 * Convert RTEMS status to a UNIX errno
 */

rtems_assoc_t errno_assoc[] = {
    { "OK",        RTEMS_SUCCESSFUL,       0 },
    { "TIMEOUT",   RTEMS_TIMEOUT,          ETIME },
    { "NO MEMORY", RTEMS_NO_MEMORY,        ENOMEM },
    { "NO DEVICE", RTEMS_UNSATISFIED,      ENOSYS },
    { 0, 0, 0 },
};

static unsigned32
rtems_libio_errno(rtems_status_code code)
{
    int rc;
    
    if ((rc = rtems_assoc_remote_by_local(errno_assoc, (unsigned32) code)))
    {
        errno = rc;
        return -1;
    }
    return 0;
}

/*
 * Convert UNIX fnctl(2) flags to ones that RTEMS drivers understand
 */

rtems_assoc_t access_modes_assoc[] = {
    { "READ",     LIBIO_FLAGS_READ,  O_RDONLY },
    { "WRITE",    LIBIO_FLAGS_WRITE, O_WRONLY },
    { "READ/WRITE", LIBIO_FLAGS_READ_WRITE, O_RDWR },
    { 0, 0, 0 },
};

rtems_assoc_t status_flags_assoc[] = {
    { "NO DELAY",  LIBIO_FLAGS_NO_DELAY,  O_NDELAY },
    { "APPEND",    LIBIO_FLAGS_APPEND,    O_APPEND },
    { "CREATE",    LIBIO_FLAGS_CREATE,    O_CREAT },
    { 0, 0, 0 },
};

static unsigned32
rtems_libio_fcntl_flags(unsigned32 fcntl_flags)
{
    unsigned32 flags = 0;
    unsigned32 access_modes;

    /*
     * Access mode is a small integer
     */
    
    access_modes = fcntl_flags & O_ACCMODE;
    fcntl_flags &= ~O_ACCMODE;
    flags = rtems_assoc_local_by_remote(access_modes_assoc, access_modes);

    /*
     * Everything else is single bits
     */

    flags |= rtems_assoc_local_by_remote_bitfield(status_flags_assoc, fcntl_flags);
    return flags;
}


static rtems_libio_t *
rtems_libio_allocate(void)
{
    rtems_libio_t *iop;
    rtems_status_code rc;
    
    rtems_semaphore_obtain(rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT);

    for (iop = rtems_libio_iops; iop <= rtems_libio_last_iop; iop++)
        if ((iop->flags & LIBIO_FLAGS_OPEN) == 0)
        {
            /*
             * Got one; create a semaphore for it
             */

            rc = rtems_semaphore_create(
              RTEMS_LIBIO_IOP_SEM(iop - rtems_libio_iops),
              1,
              RTEMS_BINARY_SEMAPHORE | RTEMS_INHERIT_PRIORITY | RTEMS_PRIORITY,
              RTEMS_NO_PRIORITY,
              &iop->sem
            );
            if (rc != RTEMS_SUCCESSFUL)
                goto failed;
            
            iop->flags = LIBIO_FLAGS_OPEN;
            goto done;
        }
    
failed:
    iop = 0;
    
done:
    rtems_semaphore_release(rtems_libio_semaphore);
    return iop;
}

static void
rtems_libio_free(rtems_libio_t *iop)
{
    rtems_semaphore_obtain(rtems_libio_semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT);

    if (iop->sem)
        rtems_semaphore_delete(iop->sem);
    (void) memset(iop, 0, sizeof(*iop));

    rtems_semaphore_release(rtems_libio_semaphore);
}

int
__rtems_open(
    const char   *pathname,
    unsigned32    flag,
    unsigned32    mode)
{
    rtems_status_code rc;
    rtems_libio_t *iop = 0;
    rtems_driver_name_t *np;
    rtems_libio_open_close_args_t args;

    if ((rc = rtems_io_lookup_name(pathname, &np)) != RTEMS_SUCCESSFUL) {
/*
      if ( rc == RTEMS_UNSATISFIED ) {
        puts( "open -- ENOSYS case" );
        assert( 0 );
      }
*/
        goto done;
    }

    iop = rtems_libio_allocate();
    if (iop == 0)
    {
        rc = RTEMS_TOO_MANY;
        goto done;
    }
    
    iop->driver = np;
    iop->pathname = (char *) pathname;
    iop->flags |= rtems_libio_fcntl_flags(flag);

    args.iop = iop;
    args.flags = iop->flags;
    args.mode = mode;

    rc = rtems_io_open(np->major, np->minor, (void *) &args);
    
done:
  
    if (rc != RTEMS_SUCCESSFUL)
    {
        if (iop)
            rtems_libio_free(iop);
        return rtems_libio_errno(rc);
    }
    
    return iop - rtems_libio_iops;
}
    
int
__rtems_close(
    int  fd
  )    
{
    rtems_status_code rc;
    rtems_driver_name_t *np;
    rtems_libio_t *iop = rtems_libio_iop(fd);
    rtems_libio_open_close_args_t args;

    rtems_libio_check_fd(fd);

    np = iop->driver;

    args.iop = iop;
    args.flags = 0;
    args.mode = 0;
    
    rc = rtems_io_close(np->major, np->minor, (void *) &args);

    if (rc != RTEMS_SUCCESSFUL)
        return rtems_libio_errno(rc);
    return 0;
}
    
int
__rtems_read(
    int       fd,
    void *    buffer,
    unsigned32 count
  )
{
    rtems_status_code rc;
    rtems_driver_name_t *np;
    rtems_libio_t *iop = rtems_libio_iop(fd);
    rtems_libio_rw_args_t args;

    rtems_libio_check_fd(fd);
    rtems_libio_check_buffer(buffer);
    rtems_libio_check_count(count);
    rtems_libio_check_permissions(iop, LIBIO_FLAGS_READ);

    np = iop->driver;

    args.iop = iop;
    args.offset = iop->offset;
    args.buffer = buffer;
    args.count = count;
    args.flags = iop->flags;
    args.bytes_moved = 0;

    rc = rtems_io_read(np->major, np->minor, (void *) &args);

    iop->offset += args.bytes_moved;

    if (rc != RTEMS_SUCCESSFUL)
        return rtems_libio_errno(rc);

    return args.bytes_moved;
}

int
__rtems_write(
    int         fd,
    const void *buffer,
    unsigned32  count
  )
{
    rtems_status_code rc;
    rtems_driver_name_t *np;
    rtems_libio_t *iop = rtems_libio_iop(fd);
    rtems_libio_rw_args_t args;

    rtems_libio_check_fd(fd);
    rtems_libio_check_buffer(buffer);
    rtems_libio_check_count(count);
    rtems_libio_check_permissions(iop, LIBIO_FLAGS_WRITE);

    np = iop->driver;

    args.iop = iop;
    args.offset = iop->offset;
    args.buffer = (void *) buffer;
    args.count = count;
    args.flags = iop->flags;
    args.bytes_moved = 0;

    rc = rtems_io_write(np->major, np->minor, (void *) &args);

    iop->offset += args.bytes_moved;

    if (rc != RTEMS_SUCCESSFUL)
        return rtems_libio_errno(rc);

    return args.bytes_moved;
}

int
__rtems_ioctl(
    int         fd,
    unsigned32  command,
    void *      buffer)
{
    rtems_status_code rc;
    rtems_driver_name_t *np;
    rtems_libio_t *iop = rtems_libio_iop(fd);
    rtems_libio_ioctl_args_t args;

    rtems_libio_check_fd(fd);

    np = iop->driver;

    args.iop = iop;
    args.command = command;
    args.buffer = buffer;

    rc = rtems_io_control(np->major, np->minor, (void *) &args);

    if (rc != RTEMS_SUCCESSFUL)
        return rtems_libio_errno(rc);

    return args.ioctl_return;
}
    
/*
 * internal only??
 */


int
__rtems_lseek(
    int                  fd,
    rtems_libio_offset_t offset,
    int                  whence
  )    
{
    rtems_libio_t *iop = rtems_libio_iop(fd);

    rtems_libio_check_fd(fd);

    switch (whence)
    {
        case SEEK_SET:
            iop->offset = offset;
            break;

        case SEEK_CUR:
            iop->offset += offset;
            break;

        case SEEK_END:
            iop->offset = iop->size - offset;
            break;

        default:
            errno = EINVAL;
            return -1;
    }
    return 0;
}