small refactor
[AROS.git] / arch / arm-native / ceboot / elf_io.c
blob2e8f353913c3c6a3d05324c17b926f093abd09d4
1 /*
2 Copyright © 2010-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: I/O routines for bootstrap ELF loader
6 Lang: english
7 */
9 #include <elfloader.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <windows.h>
15 #include "bootstrap.h"
16 #include "elf_io.h"
17 #include "filesystem.h"
19 struct ExtELFNode
21 struct ELFNode node;
22 char FullName[1]; /* We need to store the full pathname */
25 struct ELFNode *FirstELF = NULL;
26 static struct ELFNode *LastELF = (struct ELFNode *)&FirstELF;
28 int AddKernelFile(char *name)
30 struct ExtELFNode *n;
31 int l1 = 0;
32 int len = sizeof(struct ExtELFNode) + strlen(name);
34 if (*name != '\\')
36 l1 = strlen(bootstrapdir);
37 len += l1;
40 n = malloc(len);
41 if (!n)
42 return 0;
44 n->node.Next = NULL;
46 if (l1)
47 memcpy(n->FullName, bootstrapdir, l1);
48 strcpy(n->FullName + l1, name);
49 n->node.Name = namepart(n->FullName);
51 LastELF->Next = &n->node;
52 LastELF = &n->node;
54 return 1;
57 void FreeKernelList(void)
59 struct ELFNode *n, *n2;
61 for (n = FirstELF; n; n = n2)
63 n2 = n->Next;
64 free(n);
68 void *open_file(struct ELFNode *n, unsigned int *err)
70 FILE *f;
72 f = fopen(((struct ExtELFNode *)n)->FullName, "rb");
73 *err = f ? 0 : GetLastError();
75 return f;
78 void close_file(void *file)
80 fclose(file);
84 * read_block interface. we want to read from files here
86 int read_block(void *file, unsigned long offset, void *dest, unsigned long length)
88 int err;
90 err = fseek(file, offset, SEEK_SET);
91 if (err)
92 return GetLastError();
94 err = fread(dest, length, 1, file);
95 if (err == 0)
96 return GetLastError();
98 return 0;
102 * load_block also allocates the memory
104 void *load_block(void *file, unsigned long offset, unsigned long length, unsigned int *err)
106 void *dest = malloc(length);
108 if (dest)
110 *err = read_block(file, offset, dest, length);
111 if (*err)
113 free(dest);
114 return NULL;
116 *err = 0;
118 else
119 *err = GetLastError();
121 return dest;
124 void free_block(void *addr)
126 free(addr);