Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
465 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE 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.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "library.h"
26
#include "malloc.h"
27
#include "execimage.h"
28
#include "thread.h"
29
#ifdef HAVE_STORAGE
30
#include "file.h"
31
#include "dir.h"
32
#endif
33
#ifdef HAVE_BOOTFLASH
34
#include "bootflash.h"
35
#endif
36
#ifdef HAVE_BUTTON
37
#include "button.h"
38
#endif
39
 
40
 
41
struct library_handle* library_list_head;
42
struct mutex library_mutex;
43
 
44
 
45
struct library_handle* library_register(void* image, struct emcorelib_header* header)
46
{
47
    mutex_lock(&library_mutex, TIMEOUT_BLOCK);
48
    struct library_handle* h;
49
    for (h = library_list_head; h; h = h->next)
50
        if (h->lib->identifier == header->identifier && h->lib->version == header->version)
51
        {
52
            mutex_unlock(&library_mutex);
53
            return NULL;
54
        }
468 theseven 55
    if ((header->setupfunc && header->setupfunc() < 0)
56
     || (header->initfunc && header->initfunc() < 0))
465 theseven 57
    {
58
        mutex_unlock(&library_mutex);
59
        return NULL;
60
    }
61
    struct library_handle* handle = (struct library_handle*)malloc(sizeof(struct library_handle));
62
    memset(handle, 0, sizeof(struct library_handle));
63
    reownalloc(handle, (struct scheduler_thread*)handle);
64
    reownalloc(image, (struct scheduler_thread*)handle);
65
    handle->next = library_list_head;
66
    handle->lib = header;
67
    handle->alloc = image;
466 theseven 68
    library_list_head = handle;
465 theseven 69
    mutex_unlock(&library_mutex);
70
    return handle;
71
}
72
 
489 theseven 73
int library_unload(struct emcorelib_header* lib)
465 theseven 74
{
75
    mutex_lock(&library_mutex, TIMEOUT_BLOCK);
76
    int i;
77
    bool found = false;
78
    struct library_handle* h;
489 theseven 79
    struct library_handle* prev;
465 theseven 80
    for (h = library_list_head; h; h = h->next)
489 theseven 81
        if (h->lib == lib)
465 theseven 82
        {
83
            found = true;
84
            break;
85
        }
86
    if (!found)
87
    {
88
        mutex_unlock(&library_mutex);
89
        return -1;
90
    }
489 theseven 91
    for (i = 0; i < ARRAYLEN(h->users); i++)
92
        if (h->users[i])
465 theseven 93
        {
94
            mutex_unlock(&library_mutex);
95
            return -2;
96
        }
489 theseven 97
    if (h->moreusers)
98
        for (i = 0; i < h->moreusers_size / 4; i++)
99
            if (h->moreusers[i])
465 theseven 100
            {
101
                mutex_unlock(&library_mutex);
102
                return -2;
103
            }
489 theseven 104
    if (lib->shutdownfunc && lib->shutdownfunc() < 0)
465 theseven 105
    {
106
        mutex_unlock(&library_mutex);
107
        return -3;
108
    }
489 theseven 109
    if (library_list_head == h) library_list_head = h->next;
465 theseven 110
    else
111
        for (h = library_list_head; h; h = h->next)
537 theseven 112
            if (h->next->lib == lib)
465 theseven 113
            {
489 theseven 114
                prev = h->next;
115
                h->next = h->next->next;
465 theseven 116
                break;
117
            }
489 theseven 118
    library_release_all_of_thread((struct scheduler_thread*)prev);
465 theseven 119
#ifdef HAVE_STORAGE
489 theseven 120
    close_all_of_process((struct scheduler_thread*)prev);
121
    closedir_all_of_process((struct scheduler_thread*)prev);
465 theseven 122
#endif
123
#ifdef HAVE_BUTTON
489 theseven 124
    button_unregister_all_of_thread((struct scheduler_thread*)prev);
465 theseven 125
#endif
489 theseven 126
    free_all_of_thread((struct scheduler_thread*)prev);
465 theseven 127
    mutex_unlock(&library_mutex);
128
    return 0;
129
}
130
 
467 theseven 131
struct emcorelib_header* get_library_ext(uint32_t identifier, uint32_t version,
132
                                         enum library_sourcetype sourcetype, void* source,
133
                                         struct scheduler_thread* owner)
465 theseven 134
{
135
    int i;
136
    struct library_handle* h;
467 theseven 137
    struct library_handle* lib = NULL;
465 theseven 138
    mutex_lock(&library_mutex, TIMEOUT_BLOCK);
139
    for (h = library_list_head; h; h = h->next)
140
        if (h->lib->identifier == identifier &&
467 theseven 141
            h->lib->minversion <= version && h->lib->version >= version)
465 theseven 142
        {
467 theseven 143
            lib = h;
465 theseven 144
            break;
145
        }
467 theseven 146
    if (!lib)
465 theseven 147
    {
466 theseven 148
        switch (sourcetype)
149
        {
150
        case LIBSOURCE_RAM_ALLOCED:
151
        {
467 theseven 152
            lib = (struct library_handle*)execimage(source, false);
466 theseven 153
            break;
154
        }
155
 
156
        case LIBSOURCE_RAM_NEEDCOPY:
157
        {
467 theseven 158
            lib = (struct library_handle*)execimage(source, true);
466 theseven 159
            break;
160
        }
161
 
162
#ifdef HAVE_BOOTFLASH
163
        case LIBSOURCE_BOOTFLASH:
164
        {
165
            int size = bootflash_filesize((char*)source);
166
            if (size <= 0) break;
167
            void* buffer = memalign(0x10, size);
168
            if (!buffer) break;
169
            if (bootflash_read((char*)source, buffer, 0, size) != size)
170
            {
171
                free(buffer);
172
                break;
173
            }
467 theseven 174
            lib = (struct library_handle*)execimage(buffer, false);
466 theseven 175
            break;
176
        }
177
#endif
178
 
179
#ifdef HAVE_STORAGE
180
        case LIBSOURCE_FILESYSTEM:
181
        {
182
            int fd = file_open((char*)source, O_RDONLY);
183
            if (fd <= 0) break;
184
            int size = filesize(fd);
185
            if (size <= 0)
186
            {
187
                close(fd);
188
                break;
189
            }
190
            void* buffer = memalign(0x10, size);
191
            if (!buffer)
192
            {
193
                close(fd);
194
                break;
195
            }
196
            if (read(fd, buffer, size) != size)
197
            {
198
                free(buffer);
199
                close(fd);
200
                break;
201
            }
202
            close(fd);
467 theseven 203
            lib = (struct library_handle*)execimage(buffer, false);
466 theseven 204
            break;
205
        }
206
#endif
207
        }
489 theseven 208
        if (lib && (lib->lib->identifier != identifier
209
                 || lib->lib->minversion > version && lib->lib->version < version))
467 theseven 210
            lib = NULL;
211
        if (!lib)
466 theseven 212
        {
213
            mutex_unlock(&library_mutex);
214
            return NULL;
215
        }
465 theseven 216
    }
467 theseven 217
    for (i = 0; i < ARRAYLEN(lib->users); i++)
218
        if (lib->users[i] == NULL)
465 theseven 219
        {
467 theseven 220
            lib->users[i] = owner;
465 theseven 221
            mutex_unlock(&library_mutex);
467 theseven 222
            return lib->lib;
465 theseven 223
        }
467 theseven 224
    if (lib->moreusers)
225
        for (i = 0; i < lib->moreusers_size / 4; i++)
465 theseven 226
            if (h->moreusers[i] == NULL)
227
                {
228
                    h->moreusers[i] = owner;
229
                    mutex_unlock(&library_mutex);
467 theseven 230
                    return lib->lib;
465 theseven 231
                }
467 theseven 232
    void* newalloc = realloc(lib->moreusers, lib->moreusers_size + 64);
465 theseven 233
    if (!newalloc)
234
    {
235
        mutex_unlock(&library_mutex);
236
        return NULL;
237
    }
467 theseven 238
    lib->moreusers = (void**)newalloc;
239
    lib->moreusers[lib->moreusers_size / 4] = owner;
240
    lib->moreusers_size += 64;
465 theseven 241
    mutex_unlock(&library_mutex);
467 theseven 242
    return lib->lib;
465 theseven 243
}
244
 
467 theseven 245
struct emcorelib_header* get_library(uint32_t identifier, uint32_t version,
465 theseven 246
                                     enum library_sourcetype sourcetype, void* source)
247
{
467 theseven 248
    return get_library_ext(identifier, version, sourcetype, source, current_thread);
465 theseven 249
}
250
 
251
int release_library_ext(struct emcorelib_header* lib, struct scheduler_thread* owner)
252
{
253
    int i;
254
    int rc = -2;
255
    struct library_handle* h;
256
    mutex_lock(&library_mutex, TIMEOUT_BLOCK);
257
    for (h = library_list_head; h; h = h->next)
258
        if (h->lib == lib)
259
        {
260
            rc = -1;
261
            for (i = 0; i < ARRAYLEN(h->users); i++)
262
                if (h->users[i] == owner)
263
                {
264
                    h->users[i] = NULL;
265
                    rc = 0;
266
                    break;
267
                }
268
            if (rc && h->moreusers)
269
                for (i = 0; i < h->moreusers_size / 4; i++)
270
                    if (h->moreusers[i] == owner)
271
                        {
272
                            h->moreusers[i] = NULL;
273
                            rc = 0;
274
                            break;
275
                        }
276
            break;
277
        }
278
    mutex_unlock(&library_mutex);
279
    return rc;
280
}
281
 
282
int release_library(struct emcorelib_header* lib)
283
{
284
    return release_library_ext(lib, current_thread);
285
}
286
 
287
int library_release_all_of_thread(struct scheduler_thread* thread)
288
{
289
    mutex_lock(&library_mutex, TIMEOUT_BLOCK);
290
    int i;
291
    int released = 0;
292
    struct library_handle* h;
293
    for (h = library_list_head; h; h = h->next)
294
    {
295
        for (i = 0; i < ARRAYLEN(h->users); i++)
296
            if (h->users[i] == thread)
297
            {
298
                h->users[i] = NULL;
299
                released++;
300
            }
301
        if (h->moreusers)
302
            for (i = 0; i < h->moreusers_size / 4; i++)
303
                if (h->moreusers[i] == thread)
304
                    {
305
                        h->moreusers[i] = NULL;
306
                        released++;
307
                    }
308
    }
309
    mutex_unlock(&library_mutex);
310
    return released;
311
}