-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadlin.c
403 lines (352 loc) · 9.13 KB
/
loadlin.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iocslib.h>
#include <errno.h>
/*
* gzip declarations
*/
#define OF(args) args
#define STATIC static
#define INIT
#define PREBOOT
/* Pre-boot environment: included */
/* prevent inclusion of _LINUX_KERNEL_H in pre-boot environment: lots
* errors about console_printk etc... on ARM */
#define _LINUX_KERNEL_H
#include "zconf.h"
typedef unsigned char u8;
#include "inftrees.c"
#include "inffast.c"
#include "inflate.c"
#define GZIP_IOBUF_SIZE (16*1024)
static long nofill(void *buf, unsigned long size)
{
return -1;
}
/* Included from initramfs et al code */
STATIC int INIT __gunzip(unsigned char *buf, long len,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *out_buf, long out_len,
long *pos,
void(*error)(char *x)) {
u8 *zbuf;
struct z_stream_s *strm;
int rc;
rc = -1;
if (flush) {
out_len = 0x8000; /* 32 K */
out_buf = malloc(out_len);
} else {
if (!out_len)
out_len = ((size_t)~0) - (size_t)out_buf; /* no limit */
}
if (!out_buf) {
error("Out of memory while allocating output buffer");
goto gunzip_nomem1;
}
if (buf)
zbuf = buf;
else {
zbuf = malloc(GZIP_IOBUF_SIZE);
len = 0;
}
if (!zbuf) {
error("Out of memory while allocating input buffer");
goto gunzip_nomem2;
}
strm = malloc(sizeof(*strm));
if (strm == NULL) {
error("Out of memory while allocating z_stream");
goto gunzip_nomem3;
}
strm->workspace = malloc(flush ? zlib_inflate_workspacesize() :
sizeof(struct inflate_state));
if (strm->workspace == NULL) {
error("Out of memory while allocating workspace");
goto gunzip_nomem4;
}
if (!fill)
fill = nofill;
if (len == 0)
len = fill(zbuf, GZIP_IOBUF_SIZE);
/* verify the gzip header */
if (len < 10 ||
zbuf[0] != 0x1f || zbuf[1] != 0x8b || zbuf[2] != 0x08) {
if (pos)
*pos = 0;
error("Not a gzip file");
goto gunzip_5;
}
/* skip over gzip header (1f,8b,08... 10 bytes total +
* possible asciz filename)
*/
strm->next_in = zbuf + 10;
strm->avail_in = len - 10;
/* skip over asciz filename */
if (zbuf[3] & 0x8) {
do {
/*
* If the filename doesn't fit into the buffer,
* the file is very probably corrupt. Don't try
* to read more data.
*/
if (strm->avail_in == 0) {
error("header error");
goto gunzip_5;
}
--strm->avail_in;
} while (*strm->next_in++);
}
strm->next_out = out_buf;
strm->avail_out = out_len;
rc = zlib_inflateInit2(strm, -MAX_WBITS);
if (!flush) {
WS(strm)->inflate_state.wsize = 0;
WS(strm)->inflate_state.window = NULL;
}
while (rc == Z_OK) {
if (strm->avail_in == 0) {
/* TODO: handle case where both pos and fill are set */
len = fill(zbuf, GZIP_IOBUF_SIZE);
if (len < 0) {
rc = -1;
error("read error");
break;
}
strm->next_in = zbuf;
strm->avail_in = len;
}
rc = zlib_inflate(strm, 0);
/* Write any data generated */
if (flush && strm->next_out > out_buf) {
long l = strm->next_out - out_buf;
if (l != flush(out_buf, l)) {
rc = -1;
error("write error");
break;
}
strm->next_out = out_buf;
strm->avail_out = out_len;
}
/* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
if (rc == Z_STREAM_END) {
rc = 0;
break;
} else if (rc != Z_OK) {
error("uncompression error");
rc = -1;
}
}
zlib_inflateEnd(strm);
if (pos)
/* add + 8 to skip over trailer */
*pos = strm->next_in - zbuf+8;
gunzip_5:
free(strm->workspace);
gunzip_nomem4:
free(strm);
gunzip_nomem3:
if (!buf)
free(zbuf);
gunzip_nomem2:
if (flush)
free(out_buf);
gunzip_nomem1:
return rc; /* returns Z_OK (0) if successful */
}
#ifndef PREBOOT
STATIC int INIT gunzip(unsigned char *buf, long len,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *out_buf,
long *pos,
void (*error)(char *x))
{
return __gunzip(buf, len, fill, flush, out_buf, 0, pos, error);
}
#else
STATIC int INIT __decompress(unsigned char *buf, long len,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *out_buf, long out_len,
long *pos,
void (*error)(char *x))
{
return __gunzip(buf, len, fill, flush, out_buf, out_len, pos, error);
}
#endif
FILE *stream_fp;
char *kernel;
char *raw_ptr;
unsigned long raw_size;
#define KERNEL_SIZE 0x00800000
static void error(char *x)
{
puts(x);
exit(1);
}
static long read_stream(void *buf, unsigned long size)
{
return fread(buf, 1, size, stream_fp);
}
static long write_raw(void *buf, unsigned long size)
{
if (KERNEL_SIZE >= (raw_size + size)) {
memcpy(raw_ptr, buf, size);
raw_ptr += size;
raw_size += size;
fputc('.', stderr);
} else {
fprintf(stderr, "Kernel image too large\n");
size = -1;
}
return size;
}
void decompress_kernel(char *name, char *load)
{
FILE *fp;
stream_fp = fopen(name, "rb");
if (stream_fp == NULL) {
fprintf(stderr, "\n%s open failed %d\n", name, errno);
exit(1);
}
raw_ptr = load;
raw_size = 0;
__decompress(NULL, 0, read_stream, write_raw, NULL, 0, NULL, error);
fclose(stream_fp);
fputs("ok", stderr);
}
void start_kernel_000(void *kernel, unsigned long size, char *args);
void start_kernel_030(void *kernel, unsigned long size);
struct bi_record {
unsigned short tag; /* tag ID */
unsigned short size; /* size of record (in bytes) */
unsigned long data[0]; /* data */
};
struct mem_info {
unsigned long addr; /* physical address of memory chunk */
unsigned long size; /* length of memory chunk (in bytes) */
};
#define BI_LAST 0x0000 /* last record (sentinel) */
#define BI_MACHTYPE 0x0001 /* machine type (__be32) */
#define BI_CPUTYPE 0x0002 /* cpu type (__be32) */
#define BI_FPUTYPE 0x0003 /* fpu type (__be32) */
#define BI_MMUTYPE 0x0004 /* mmu type (__be32) */
#define BI_MEMCHUNK 0x0005 /* memory chunk address and size */
/* (struct mem_info) */
#define BI_RAMDISK 0x0006 /* ramdisk address and size */
/* (struct mem_info) */
#define BI_COMMAND_LINE 0x0007 /* kernel command line parameters */
/* (string) */
#define MACH_X68000 14
#define CPUB_68030 1
#define CPUB_68040 2
#define CPUB_68060 3
#define CPU_68030 (1 << CPUB_68030)
#define CPU_68040 (1 << CPUB_68040)
#define CPU_68060 (1 << CPUB_68060)
#define MMUB_68030 1 /* Internal MMU */
#define MMUB_68040 2 /* Internal MMU */
#define MMUB_68060 3 /* Internal MMU */
#define MMU_68030 (1 << MMUB_68030)
#define MMU_68040 (1 << MMUB_68040)
#define MMU_68060 (1 << MMUB_68060)
#define FPUB_68881 0
#define FPUB_68882 1
#define FPUB_68040 2 /* Internal FPU */
#define FPUB_68060 3 /* Internal FPU */
#define FPUB_SUNFPA 4 /* Sun-3 FPA */
#define FPUB_COLDFIRE 5 /* ColdFire FPU */
#define FPU_68881 (1 << FPUB_68881)
#define FPU_68882 (1 << FPUB_68882)
#define FPU_68040 (1 << FPUB_68040)
#define FPU_68060 (1 << FPUB_68060)
#define BI(_tag, _size) \
do { \
bi = bi_ptr; \
bi->tag = _tag; \
bi->size = 4 + _size; \
bi_ptr += 4 + _size; \
} while(0)
unsigned long build_bootinfo(void *kernel, unsigned long size, char *args)
{
void *bi_ptr;
struct bi_record *bi;
char *cmdline;
size += 0x1000;
size &= ~0xfff;
bi_ptr = kernel + size;
BI(BI_MACHTYPE, 4);
bi->data[0] = MACH_X68000;
BI(BI_FPUTYPE, 4);
bi->data[0] = FPU_68882;
BI(BI_MMUTYPE, 4);
bi->data[0] = MMU_68030;
BI(BI_CPUTYPE, 4);
bi->data[0] = CPU_68030;
BI(BI_MEMCHUNK, 8);
bi->data[0] = 0x00000000;
bi->data[1] = 0x00c00000;
BI(BI_COMMAND_LINE, 256);
strcpy((char *)(bi->data), args);
BI(BI_LAST, 0);
return bi_ptr - kernel + 256;
}
void enter_kernel(int video)
{
if (video) {
fprintf(stderr, "\x1b[>5h");
_iocs_crtmod(0x10);
_iocs_g_clr_on();
}
*(volatile char *)0xe8801d = 0x00;
*(volatile char *)0xe98001 = 9;
*(volatile char *)0xe98001 = 0x40;
}
int main(int argc, char *argv[])
{
char args[512];
int i;
int video = 0;
int cpu;
int sp;
if (argc < 2) {
printf("loadlin <kernel> <bootargs> ...\n");
exit(1);
}
kernel = malloc(KERNEL_SIZE);
if (kernel == NULL) {
fprintf(stderr, "malloc failed\n");
exit(1);
}
fprintf(stderr, "Uncompressing kernel");
decompress_kernel(argv[1], kernel);
fprintf(stderr, "\nKernel size=%d\n", raw_size);
memset(args, 0, sizeof(args));
for (i = 2; i < argc; i++) {
if (strncmp(argv[i], "video", 5) == 0)
video = 1;
strcat(args, argv[i]);
strcat(args, " ");
}
fprintf(stderr, "Kernel args=%s\n", args);
sp = _dos_super(0);
cpu = *(volatile char *)0xcbc;
switch (cpu) {
case 0:
enter_kernel(video);
start_kernel_000(kernel, raw_size, args);
break;
case 3:
raw_size += build_bootinfo(kernel, raw_size, args);
enter_kernel(video);
start_kernel_030(kernel, raw_size);
break;
default:
fprintf(stderr, "Unsupported CPU\n");
}
_dos_super(sp);
return 0;
}