Skip to content

Commit 5dab8c1

Browse files
author
Marek
committed
Add stub for loading ELF binaries
Linux is unaffected by this since the resulting kernel (that blob that's actually loaded) isn't in ELF format and what's better, the decompressor itself handles all the dirty work (relocation...). Some operating systems have the resulting kernel as an ELF binary though so that's why an ELF loader would be nice to have (to load them).
1 parent 3fecae3 commit 5dab8c1

4 files changed

Lines changed: 81 additions & 5 deletions

File tree

‎arm/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CC = arm-palmos-gcc
22
CFLAGS = -Wall -O -s -I../include ${DEFINES}
33
LDFLAGS = -nostartfiles
4-
OBJS = arm.o atag.o boot.o cpu.o debug.o
4+
OBJS = arm.o atag.o boot.o cpu.o debug.o elf.o
55

66
all: cocoboot.arm
77

‎arm/boot.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "mem.h"
2121
#include "regs.h"
2222
#include "cpu.h"
23+
#include "elf.h"
2324

2425
#define MACH_TYPE_T3XSCALE 829
2526

@@ -81,16 +82,19 @@ UInt32 boot_linux(ArmGlobals *g, void *kernel, UInt32 kernel_size,
8182
ArmGlobals *pg=NULL;
8283
void *vstack=NULL, *pstack=NULL;
8384
void *vphys_jump=NULL, *pphys_jump=NULL;
84-
85+
int elf = 0;
86+
8587
if(!kernel || !cmdline) {
8688
return 0xc0ffee;
8789
}
8890
UInt32 initrd_offset;
8991

92+
/* We do this before tinkering with hardware, it's safer */
93+
elf = test_elf((UInt32 *)kernel);
94+
9095
/* since we're going to turn off the MMU, we need to translate
9196
* all out pointers to physical addresses.
9297
*/
93-
9498
kernel = (void *)virt_to_phys(g, (UInt32) kernel);
9599
cmdline = (char *)virt_to_phys(g, (UInt32) cmdline);
96100

@@ -179,8 +183,12 @@ UInt32 boot_linux(ArmGlobals *g, void *kernel, UInt32 kernel_size,
179183
copy_image((void*)(pg->ram_base + initrd_offset), initrd, initrd_size);
180184
}
181185

182-
/* bring on the penguin! */
183-
jump_to_kernel(kernel, pg->ram_base + TAG_OFFSET, pg->mach_num);
186+
if (elf)
187+
/* handle additional stuff necessary to load ELF kernel */
188+
relocate_elf(kernel, kernel_size);
189+
else
190+
/* bring on the penguin! */
191+
jump_to_kernel(kernel, pg->ram_base + TAG_OFFSET, pg->mach_num);
184192

185193
return 0xe4; /* sadly, this return will never be executed */
186194
}

‎arm/elf.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <PalmOS.h>
2+
#include "elf.h"
3+
4+
/* test_elf()
5+
*
6+
* Check if supplied image is in ELF format or not.
7+
* */
8+
int test_elf(UInt32* img)
9+
{
10+
unsigned long elfmagic = ELFMAG0 | (ELFMAG1 << 8) |
11+
(ELFMAG2 << 16) | (ELFMAG3 << 24);
12+
return (img[0] == elfmagic);
13+
}
14+
15+
/* relocate_elf()
16+
*
17+
* Relocate the ELF file in memory ... ToDo
18+
* */
19+
void relocate_elf(UInt32 *img, UInt32 size)
20+
{
21+
for(;;);
22+
}

‎include/elf.h‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* elf32_hdr.e_ident bytes definition */
2+
#define EI_MAG0 0
3+
#define EI_MAG1 1
4+
#define EI_MAG2 2
5+
#define EI_MAG3 3
6+
#define EI_CLASS 4
7+
#define EI_DATA 5
8+
#define EI_VERSION 6
9+
#define EI_PAD 7
10+
#define EI_NIDENT 16
11+
12+
/* ELF magic numbers - EI_MAG[0-3] */
13+
#define ELFMAG0 0x7f
14+
#define ELFMAG1 'E'
15+
#define ELFMAG2 'L'
16+
#define ELFMAG3 'F'
17+
18+
/* ELF class - EI_CLASS */
19+
#define ELFCLASSNONE 0
20+
#define ELFCLASS32 1
21+
#define ELFCLASS64 2
22+
23+
/* ELF data format - EI_DATA */
24+
#define ELFDATANONE 0
25+
#define ELFDATA2LSB 1
26+
#define ELFDATA2MSB 2
27+
28+
struct elf32_hdr {
29+
unsigned char e_ident[EI_NIDENT];
30+
UInt16 e_type;
31+
UInt16 e_machine;
32+
UInt32 e_version;
33+
UInt32 e_entry; /* Entry point */
34+
UInt32 e_phoff;
35+
UInt32 e_shoff;
36+
UInt32 e_flags;
37+
UInt16 e_ehsize;
38+
UInt16 e_phentsize;
39+
UInt16 e_phnum;
40+
UInt16 e_shentsize;
41+
UInt16 e_shnum;
42+
UInt16 e_shstrndx;
43+
};
44+
45+
int test_elf(UInt32 *img);
46+
void relocate_elf(UInt32 *kernel, UInt32 size);

0 commit comments

Comments
 (0)