]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - arch/ia64/hp/sim/boot/bootloader.c
a7bed60b69f9e8de9a49944e22d03fb388ae93c7
[linux-2.6.git] / arch / ia64 / hp / sim / boot / bootloader.c
1 /*
2  * arch/ia64/hp/sim/boot/bootloader.c
3  *
4  * Loads an ELF kernel.
5  *
6  * Copyright (C) 1998-2003 Hewlett-Packard Co
7  *      David Mosberger-Tang <davidm@hpl.hp.com>
8  *      Stephane Eranian <eranian@hpl.hp.com>
9  *
10  * 01/07/99 S.Eranian modified to pass command line arguments to kernel
11  */
12 struct task_struct;     /* forward declaration for elf.h */
13
14 #include <linux/config.h>
15 #include <linux/elf.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18
19 #include <asm/elf.h>
20 #include <asm/intrinsics.h>
21 #include <asm/pal.h>
22 #include <asm/pgtable.h>
23 #include <asm/sal.h>
24 #include <asm/system.h>
25
26 #include "ssc.h"
27
28 struct disk_req {
29         unsigned long addr;
30         unsigned len;
31 };
32
33 /* SSC_WAIT_COMPLETION appears to want this large alignment.  gcc < 4
34  * seems to give it by default, however gcc > 4 is smarter and may
35  * not.
36  */
37 struct disk_stat {
38         int fd;
39         unsigned count;
40 } __attribute__ ((aligned (16)));
41
42 extern void jmp_to_kernel (unsigned long bp, unsigned long e_entry);
43 extern struct ia64_boot_param *sys_fw_init (const char *args, int arglen);
44 extern void debug_break (void);
45
46 static void
47 cons_write (const char *buf)
48 {
49         unsigned long ch;
50
51         while ((ch = *buf++) != '\0') {
52                 ssc(ch, 0, 0, 0, SSC_PUTCHAR);
53                 if (ch == '\n')
54                   ssc('\r', 0, 0, 0, SSC_PUTCHAR);
55         }
56 }
57
58 #define MAX_ARGS 32
59
60 void
61 start_bootloader (void)
62 {
63         static char mem[4096];
64         static char buffer[1024];
65         unsigned long off;
66         int fd, i;
67         struct disk_req req;
68         struct disk_stat stat;
69         struct elfhdr *elf;
70         struct elf_phdr *elf_phdr;      /* program header */
71         unsigned long e_entry, e_phoff, e_phnum;
72         register struct ia64_boot_param *bp;
73         char *kpath, *args;
74         long arglen = 0;
75
76         ssc(0, 0, 0, 0, SSC_CONSOLE_INIT);
77
78         /*
79          * S.Eranian: extract the commandline argument from the simulator
80          *
81          * The expected format is as follows:
82          *
83          *      kernelname args...
84          *
85          * Both are optional but you can't have the second one without the first.
86          */
87         arglen = ssc((long) buffer, 0, 0, 0, SSC_GET_ARGS);
88
89         kpath = "vmlinux";
90         args = buffer;
91         if (arglen > 0) {
92                 kpath = buffer;
93                 while (*args != ' ' && *args != '\0')
94                         ++args, --arglen;
95                 if (*args == ' ')
96                         *args++ = '\0', --arglen;
97         }
98
99         if (arglen <= 0) {
100                 args = "";
101                 arglen = 1;
102         }
103
104         fd = ssc((long) kpath, 1, 0, 0, SSC_OPEN);
105
106         if (fd < 0) {
107                 cons_write(kpath);
108                 cons_write(": file not found, reboot now\n");
109                 for(;;);
110         }
111         stat.fd = fd;
112         off = 0;
113
114         req.len = sizeof(mem);
115         req.addr = (long) mem;
116         ssc(fd, 1, (long) &req, off, SSC_READ);
117         ssc((long) &stat, 0, 0, 0, SSC_WAIT_COMPLETION);
118
119         elf = (struct elfhdr *) mem;
120         if (elf->e_ident[0] == 0x7f && strncmp(elf->e_ident + 1, "ELF", 3) != 0) {
121                 cons_write("not an ELF file\n");
122                 return;
123         }
124         if (elf->e_type != ET_EXEC) {
125                 cons_write("not an ELF executable\n");
126                 return;
127         }
128         if (!elf_check_arch(elf)) {
129                 cons_write("kernel not for this processor\n");
130                 return;
131         }
132
133         e_entry = elf->e_entry;
134         e_phnum = elf->e_phnum;
135         e_phoff = elf->e_phoff;
136
137         cons_write("loading ");
138         cons_write(kpath);
139         cons_write("...\n");
140
141         for (i = 0; i < e_phnum; ++i) {
142                 req.len = sizeof(*elf_phdr);
143                 req.addr = (long) mem;
144                 ssc(fd, 1, (long) &req, e_phoff, SSC_READ);
145                 ssc((long) &stat, 0, 0, 0, SSC_WAIT_COMPLETION);
146                 if (stat.count != sizeof(*elf_phdr)) {
147                         cons_write("failed to read phdr\n");
148                         return;
149                 }
150                 e_phoff += sizeof(*elf_phdr);
151
152                 elf_phdr = (struct elf_phdr *) mem;
153
154                 if (elf_phdr->p_type != PT_LOAD)
155                         continue;
156
157                 req.len = elf_phdr->p_filesz;
158                 req.addr = __pa(elf_phdr->p_paddr);
159                 ssc(fd, 1, (long) &req, elf_phdr->p_offset, SSC_READ);
160                 ssc((long) &stat, 0, 0, 0, SSC_WAIT_COMPLETION);
161                 memset((char *)__pa(elf_phdr->p_paddr) + elf_phdr->p_filesz, 0,
162                        elf_phdr->p_memsz - elf_phdr->p_filesz);
163         }
164         ssc(fd, 0, 0, 0, SSC_CLOSE);
165
166         cons_write("starting kernel...\n");
167
168         /* fake an I/O base address: */
169         ia64_setreg(_IA64_REG_AR_KR0, 0xffffc000000UL);
170
171         bp = sys_fw_init(args, arglen);
172
173         ssc(0, (long) kpath, 0, 0, SSC_LOAD_SYMBOLS);
174
175         debug_break();
176         jmp_to_kernel((unsigned long) bp, e_entry);
177
178         cons_write("kernel returned!\n");
179         ssc(-1, 0, 0, 0, SSC_EXIT);
180 }