Indentation fix, cleanup.
[AROS.git] / arch / all-hosted / bootstrap / elf_io.c
blob46bb52a7d56e87badbf24efd3c9031c48bb6dc7d
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <elfloader.h>
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
12 #include "elf_io.h"
13 #include "support.h"
15 #include <aros/config.h>
17 struct ExtELFNode
19 struct ELFNode node;
20 char FullName[1]; /* We need to store the full pathname */
23 struct ELFNode *FirstELF = NULL;
24 static struct ELFNode *LastELF = (struct ELFNode *)&FirstELF;
26 int AddKernelFile(char *name)
28 struct ExtELFNode *n;
30 n = malloc(sizeof(struct ExtELFNode) + strlen(name) + 1);
31 if (!n)
32 return 0;
34 n->node.Next = NULL;
35 strcpy(n->FullName, name);
36 #if AROS_MODULES_DEBUG
37 n->node.Name = n->FullName;
38 #else
39 n->node.Name = namepart(n->FullName);
40 #endif
42 LastELF->Next = &n->node;
43 LastELF = &n->node;
45 return 1;
48 void FreeKernelList(void)
50 struct ELFNode *n, *n2;
52 for (n = FirstELF; n; n = n2)
54 n2 = n->Next;
55 free(n);
58 /* Reset list pointers. The list can ocassionally be reused (on Android, for example) */
59 LastELF = (struct ELFNode *)&FirstELF;
62 void *open_file(struct ELFNode *n, unsigned int *err)
64 FILE *f;
66 f = fopen(((struct ExtELFNode *)n)->FullName, "rb");
67 *err = f ? 0 : errno;
69 return f;
72 void close_file(void *file)
74 fclose(file);
78 * read_block interface. we want to read from files here
80 int read_block(void *file, unsigned long offset, void *dest, unsigned long length)
82 int err;
84 err = fseek(file, offset, SEEK_SET);
85 if (err)
86 return errno;
88 err = fread(dest, length, 1, file);
89 if (err == 0)
90 return errno;
92 return 0;
96 * load_block also allocates the memory
98 void *load_block(void *file, unsigned long offset, unsigned long length, unsigned int *err)
100 void *dest = malloc(length);
102 if (dest)
104 *err = read_block(file, offset, dest, length);
105 if (*err)
107 free(dest);
108 return NULL;
110 *err = 0;
112 else
113 *err = errno;
115 return dest;
118 void free_block(void *addr)
120 free(addr);