1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <common.h>
32#include <asm/io.h>
33#include <asm/ptrace.h>
34#include <asm/zimage.h>
35#include <asm/realmode.h>
36#include <asm/byteorder.h>
37
38
39
40
41
42
43
44
45
46
47#define DEFAULT_SETUP_BASE 0x90000
48#define COMMAND_LINE_OFFSET 0x9000
49#define HEAP_END_OFFSET 0x8e00
50
51#define COMMAND_LINE_SIZE 2048
52
53static void build_command_line(char *command_line, int auto_boot)
54{
55 char *env_command_line;
56
57 command_line[0] = '\0';
58
59 env_command_line = getenv("bootargs");
60
61
62 if (NULL == strstr(env_command_line, "console=")) {
63 if (0==strcmp(getenv("stdout"), "serial")) {
64
65
66 sprintf(command_line, "console=ttyS0,%s ",
67 getenv("baudrate"));
68 }
69 }
70
71 if (auto_boot) {
72 strcat(command_line, "auto ");
73 }
74
75 if (NULL != env_command_line) {
76 strcat(command_line, env_command_line);
77 }
78
79
80 printf("Kernel command line: \"%s\"\n", command_line);
81}
82
83void *load_zimage(char *image, unsigned long kernel_size,
84 unsigned long initrd_addr, unsigned long initrd_size,
85 int auto_boot)
86{
87 void *setup_base;
88 int setup_size;
89 int bootproto;
90 int big_image;
91 void *load_address;
92
93
94 setup_base = (void*)DEFAULT_SETUP_BASE;
95
96 if (KERNEL_MAGIC != *(u16*)(image + BOOT_FLAG_OFF)) {
97 printf("Error: Invalid kernel magic (found 0x%04x, expected 0xaa55)\n",
98 *(u16*)(image + BOOT_FLAG_OFF));
99 return 0;
100 }
101
102
103
104 if (KERNEL_V2_MAGIC == *(u32*)(image+HEADER_OFF)) {
105 bootproto = *(u16*)(image+VERSION_OFF);
106 } else {
107
108 bootproto = 0x0100;
109 }
110
111
112 if (0 == *(u8*)(image + SETUP_SECTS_OFF)) {
113 setup_size = 5 * 512;
114 } else {
115 setup_size = (*(u8*)(image + SETUP_SECTS_OFF) + 1) * 512;
116 }
117
118 if (setup_size > SETUP_MAX_SIZE) {
119 printf("Error: Setup is too large (%d bytes)\n", setup_size);
120 }
121
122
123 big_image = (bootproto >= 0x0200) && (*(u8*)(image + LOADFLAGS_OFF) & BIG_KERNEL_FLAG);
124
125
126 load_address = (void*)(big_image ? BZIMAGE_LOAD_ADDR:ZIMAGE_LOAD_ADDR);
127
128
129 memmove(setup_base, image, setup_size);
130
131 printf("Using boot protocol version %x.%02x\n",
132 (bootproto & 0xff00) >> 8, bootproto & 0xff);
133
134
135 if (bootproto == 0x0100) {
136
137 *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
138 *(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
139
140
141
142
143 if ((u32)setup_base != 0x90000) {
144
145 memmove((void*)0x90000, setup_base, setup_size);
146
147 memmove((void*)0x99000, setup_base+COMMAND_LINE_OFFSET,
148 COMMAND_LINE_SIZE);
149
150 setup_base = (void*)0x90000;
151 }
152
153
154 memset((void*)0x90000 + setup_size, 0, SETUP_MAX_SIZE-setup_size);
155 }
156
157 if (bootproto >= 0x0200) {
158 *(u8*)(setup_base + TYPE_OF_LOADER_OFF) = 0xff;
159 printf("Linux kernel version %s\n",
160 (char*)(setup_base + SETUP_START_OFFSET +
161 *(u16*)(setup_base + START_SYS_OFF + 2)));
162
163 if (initrd_addr) {
164 printf("Initial RAM disk at linear address 0x%08lx, size %ld bytes\n",
165 initrd_addr, initrd_size);
166
167 *(u32*)(setup_base + RAMDISK_IMAGE_OFF) = initrd_addr;
168 *(u32*)(setup_base + RAMDISK_SIZE_OFF)=initrd_size;
169 }
170 }
171
172 if (bootproto >= 0x0201) {
173 *(u16*)(setup_base + HEAP_END_PTR_OFF) = HEAP_END_OFFSET;
174
175
176 *(u8*)(setup_base + LOADFLAGS_OFF) =
177 *(u8*)(setup_base + LOADFLAGS_OFF) | HEAP_FLAG;
178 }
179
180 if (bootproto >= 0x0202) {
181 *(u32*)(setup_base + CMD_LINE_PTR_OFF) = (u32)setup_base + COMMAND_LINE_OFFSET;
182 } else if (bootproto >= 0x0200) {
183 *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
184 *(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
185 *(u16*)(setup_base + SETUP_MOVE_SIZE_OFF) = 0x9100;
186 }
187
188
189 if (big_image) {
190 if ((kernel_size - setup_size) > BZIMAGE_MAX_SIZE) {
191 printf("Error: bzImage kernel too big! (size: %ld, max: %d)\n",
192 kernel_size - setup_size, BZIMAGE_MAX_SIZE);
193 return 0;
194 }
195
196 } else if ((kernel_size - setup_size) > ZIMAGE_MAX_SIZE) {
197 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
198 kernel_size - setup_size, ZIMAGE_MAX_SIZE);
199 return 0;
200 }
201
202
203 build_command_line(setup_base + COMMAND_LINE_OFFSET, auto_boot);
204
205 printf("Loading %czImage at address 0x%08x (%ld bytes)\n", big_image ? 'b' : ' ',
206 (u32)load_address, kernel_size - setup_size);
207
208
209 memmove(load_address, image + setup_size, kernel_size - setup_size);
210
211
212 return setup_base;
213}
214
215void boot_zimage(void *setup_base)
216{
217 struct pt_regs regs;
218
219 memset(®s, 0, sizeof(struct pt_regs));
220 regs.xds = (u32)setup_base >> 4;
221 regs.xss = 0x9000;
222 regs.esp = 0x9000;
223 regs.eflags = 0;
224 enter_realmode(((u32)setup_base+SETUP_START_OFFSET)>>4, 0, ®s, ®s);
225}
226