1 |
786 |
skrzyp |
/* =================================================================
|
2 |
|
|
*
|
3 |
|
|
* loader_fs.c
|
4 |
|
|
*
|
5 |
|
|
* Routines to read a library from a file system.
|
6 |
|
|
*
|
7 |
|
|
* =================================================================
|
8 |
|
|
* ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
* -------------------------------------------
|
10 |
|
|
* This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
* Copyright (C) 2005, 2008 Free Software Foundation, Inc.
|
12 |
|
|
*
|
13 |
|
|
* eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
* the terms of the GNU General Public License as published by the Free
|
15 |
|
|
* Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
* version.
|
17 |
|
|
*
|
18 |
|
|
* eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
* for more details.
|
22 |
|
|
*
|
23 |
|
|
* You should have received a copy of the GNU General Public License
|
24 |
|
|
* along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
*
|
27 |
|
|
* As a special exception, if other files instantiate templates or use
|
28 |
|
|
* macros or inline functions from this file, or you compile this file
|
29 |
|
|
* and link it with other works to produce a work based on this file,
|
30 |
|
|
* this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
* the GNU General Public License. However the source code for this file
|
32 |
|
|
* must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
* General Public License v2.
|
34 |
|
|
*
|
35 |
|
|
* This exception does not invalidate any other reasons why a work based
|
36 |
|
|
* on this file might be covered by the GNU General Public License.
|
37 |
|
|
* -------------------------------------------
|
38 |
|
|
* ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
* =================================================================
|
40 |
|
|
* #####DESCRIPTIONBEGIN####
|
41 |
|
|
*
|
42 |
|
|
* Author(s): Anthony Tonizzo (atonizzo@gmail.com)
|
43 |
|
|
* Contributors: nickg@ecoscentric.com
|
44 |
|
|
* Date: 2005-05-13
|
45 |
|
|
* Purpose:
|
46 |
|
|
* Description:
|
47 |
|
|
*
|
48 |
|
|
* ####DESCRIPTIONEND####
|
49 |
|
|
*
|
50 |
|
|
* =================================================================
|
51 |
|
|
*/
|
52 |
|
|
|
53 |
|
|
#include <cyg/infra/diag.h> // For diagnostic printing.
|
54 |
|
|
#include <pkgconf/io_fileio.h>
|
55 |
|
|
#include <sys/stat.h>
|
56 |
|
|
#include <stdlib.h>
|
57 |
|
|
#include <stdio.h>
|
58 |
|
|
#include <string.h>
|
59 |
|
|
|
60 |
|
|
#include <pkgconf/objloader.h>
|
61 |
|
|
#include <cyg/objloader/elf.h>
|
62 |
|
|
#include <cyg/objloader/objelf.h>
|
63 |
|
|
#include <cyg/objloader/loader_fs.h>
|
64 |
|
|
|
65 |
|
|
static size_t
|
66 |
|
|
cyg_ldr_fs_read(PELF_OBJECT p, size_t s, size_t n, void *mem)
|
67 |
|
|
{
|
68 |
|
|
return fread(mem, s, n, (FILE*)p->ptr);
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
static cyg_int32
|
72 |
|
|
cyg_ldr_fs_seek(PELF_OBJECT p, cyg_uint32 offs)
|
73 |
|
|
{
|
74 |
|
|
return fseek((FILE*)p->ptr, offs, SEEK_SET);
|
75 |
|
|
}
|
76 |
|
|
|
77 |
|
|
static cyg_int32
|
78 |
|
|
cyg_ldr_fs_close(PELF_OBJECT p)
|
79 |
|
|
{
|
80 |
|
|
return fclose((FILE*)p->ptr);
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
PELF_OBJECT
|
84 |
|
|
cyg_ldr_open_library_fs(char *ptr)
|
85 |
|
|
{
|
86 |
|
|
FILE *fp = fopen(ptr, "rb");
|
87 |
|
|
if (fp == NULL)
|
88 |
|
|
{
|
89 |
|
|
cyg_ldr_last_error = "FILE NOT FOUND";
|
90 |
|
|
return (void*)0;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
// Create a file object to keep track of this library.
|
94 |
|
|
PELF_OBJECT e_obj = (PELF_OBJECT)malloc(sizeof(ELF_OBJECT));
|
95 |
|
|
CYG_ASSERT(e_obj != 0, "Cannot malloc() e_obj");
|
96 |
|
|
if (e_obj == 0)
|
97 |
|
|
{
|
98 |
|
|
cyg_ldr_last_error = "ERROR IN MALLOC";
|
99 |
|
|
fclose(fp);
|
100 |
|
|
return (void*)0;
|
101 |
|
|
}
|
102 |
|
|
memset(e_obj, 0, sizeof(ELF_OBJECT));
|
103 |
|
|
e_obj->ptr = (CYG_ADDRWORD)fp;
|
104 |
|
|
e_obj->mode = CYG_LDR_MODE_FILESYSTEM;
|
105 |
|
|
|
106 |
|
|
// Handlers for the file system open.
|
107 |
|
|
e_obj->read = cyg_ldr_fs_read;
|
108 |
|
|
e_obj->seek = cyg_ldr_fs_seek;
|
109 |
|
|
e_obj->close = cyg_ldr_fs_close;
|
110 |
|
|
return e_obj;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
void
|
114 |
|
|
cyg_ldr_close_library_fs(PELF_OBJECT p)
|
115 |
|
|
{
|
116 |
|
|
}
|
117 |
|
|
|