OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [arch/] [i386/] [boot/] [tools/] [build.c] - Blame information for rev 1777

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1623 jcastillo
/*
2
 *  linux/tools/build.c
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5
 */
6
 
7
/*
8
 * This file builds a disk-image from three different files:
9
 *
10
 * - bootsect: exactly 512 bytes of 8086 machine code, loads the rest
11
 * - setup: 8086 machine code, sets up system parm
12
 * - system: 80386 code for actual system
13
 *
14
 * It does some checking that all files are of the correct type, and
15
 * just writes the result to stdout, removing headers and padding to
16
 * the right amount. It also writes some system data to stderr.
17
 */
18
 
19
/*
20
 * Changes by tytso to allow root device specification
21
 * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
22
 * Cross compiling fixes by Gertjan van Wingerde, July 1996
23
 */
24
 
25
#include <stdio.h>      /* fprintf */
26
#include <string.h>
27
#include <stdlib.h>     /* contains exit */
28
#include <sys/types.h>  /* unistd.h needs this */
29
#include <sys/stat.h>
30
#include <sys/sysmacros.h>
31
#include <unistd.h>     /* contains read/write */
32
#include <fcntl.h>
33
#include <linux/config.h>
34
#include <linux/a.out.h>
35
#include <errno.h>
36
 
37
 
38
#define MINIX_HEADER 32
39
 
40
#define N_MAGIC_OFFSET 1024
41
#ifndef __BFD__
42
static int GCC_HEADER = sizeof(struct exec);
43
#endif
44
 
45
#ifdef __BIG_KERNEL__
46
#define SYS_SIZE 0xffff
47
#else
48
#define SYS_SIZE DEF_SYSSIZE
49
#endif
50
 
51
#define DEFAULT_MAJOR_ROOT 0
52
#define DEFAULT_MINOR_ROOT 0
53
 
54
/* max nr of sectors of setup: don't change unless you also change
55
 * bootsect etc */
56
#define SETUP_SECTS 4
57
 
58
#define STRINGIFY(x) #x
59
 
60
typedef union {
61
        int i;
62
        long l;
63
        short s[2];
64
        char b[4];
65
} conv;
66
 
67
long intel_long(long l)
68
{
69
        conv t;
70
 
71
        t.b[0] = l & 0xff; l >>= 8;
72
        t.b[1] = l & 0xff; l >>= 8;
73
        t.b[2] = l & 0xff; l >>= 8;
74
        t.b[3] = l & 0xff; l >>= 8;
75
        return t.l;
76
}
77
 
78
int intel_int(int i)
79
{
80
        conv t;
81
 
82
        t.b[0] = i & 0xff; i >>= 8;
83
        t.b[1] = i & 0xff; i >>= 8;
84
        t.b[2] = i & 0xff; i >>= 8;
85
        t.b[3] = i & 0xff; i >>= 8;
86
        return t.i;
87
}
88
 
89
short intel_short(short l)
90
{
91
        conv t;
92
 
93
        t.b[0] = l & 0xff; l >>= 8;
94
        t.b[1] = l & 0xff; l >>= 8;
95
        return t.s[0];
96
}
97
 
98
void die(const char * str)
99
{
100
        fprintf(stderr,"%s\n",str);
101
        exit(1);
102
}
103
 
104
void usage(void)
105
{
106
        die("Usage: build bootsect setup system [rootdev] [> image]");
107
}
108
 
109
int main(int argc, char ** argv)
110
{
111
        int i,c,id,sz,tmp_int;
112
        unsigned long sys_size, tmp_long;
113
        char buf[1024];
114
#ifndef __BFD__
115
        struct exec *ex = (struct exec *)buf;
116
#endif
117
        char major_root, minor_root;
118
        struct stat sb;
119
        unsigned char setup_sectors;
120
 
121
        if ((argc < 4) || (argc > 5))
122
                usage();
123
        if (argc > 4) {
124
                if (!strcmp(argv[4], "CURRENT")) {
125
                        if (stat("/", &sb)) {
126
                                perror("/");
127
                                die("Couldn't stat /");
128
                        }
129
                        major_root = major(sb.st_dev);
130
                        minor_root = minor(sb.st_dev);
131
                } else if (strcmp(argv[4], "FLOPPY")) {
132
                        if (stat(argv[4], &sb)) {
133
                                perror(argv[4]);
134
                                die("Couldn't stat root device.");
135
                        }
136
                        major_root = major(sb.st_rdev);
137
                        minor_root = minor(sb.st_rdev);
138
                } else {
139
                        major_root = 0;
140
                        minor_root = 0;
141
                }
142
        } else {
143
                major_root = DEFAULT_MAJOR_ROOT;
144
                minor_root = DEFAULT_MINOR_ROOT;
145
        }
146
        fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
147
        for (i=0;i<sizeof buf; i++) buf[i]=0;
148
        if ((id=open(argv[1],O_RDONLY,0))<0)
149
                die("Unable to open 'boot'");
150
        if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
151
                die("Unable to read header of 'boot'");
152
        if (((long *) buf)[0]!=intel_long(0x04100301))
153
                die("Non-Minix header of 'boot'");
154
        if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
155
                die("Non-Minix header of 'boot'");
156
        if (((long *) buf)[3] != 0)
157
                die("Illegal data segment in 'boot'");
158
        if (((long *) buf)[4] != 0)
159
                die("Illegal bss in 'boot'");
160
        if (((long *) buf)[5] != 0)
161
                die("Non-Minix header of 'boot'");
162
        if (((long *) buf)[7] != 0)
163
                die("Illegal symbol table in 'boot'");
164
        i=read(id,buf,sizeof buf);
165
        fprintf(stderr,"Boot sector %d bytes.\n",i);
166
        if (i != 512)
167
                die("Boot block must be exactly 512 bytes");
168
        if ((*(unsigned short *)(buf+510)) != (unsigned short)intel_short(0xAA55))
169
                die("Boot block hasn't got boot flag (0xAA55)");
170
        buf[508] = (char) minor_root;
171
        buf[509] = (char) major_root;
172
        i=write(1,buf,512);
173
        if (i!=512)
174
                die("Write call failed");
175
        close (id);
176
 
177
        if ((id=open(argv[2],O_RDONLY,0))<0)
178
                die("Unable to open 'setup'");
179
        if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
180
                die("Unable to read header of 'setup'");
181
        if (((long *) buf)[0]!=intel_long(0x04100301))
182
                die("Non-Minix header of 'setup'");
183
        if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
184
                die("Non-Minix header of 'setup'");
185
        if (((long *) buf)[3] != 0)
186
                die("Illegal data segment in 'setup'");
187
        if (((long *) buf)[4] != 0)
188
                die("Illegal bss in 'setup'");
189
        if (((long *) buf)[5] != 0)
190
                die("Non-Minix header of 'setup'");
191
        if (((long *) buf)[7] != 0)
192
                die("Illegal symbol table in 'setup'");
193
        for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
194
#ifdef __BIG_KERNEL__
195
        {
196
                if (!i) {
197
                        /* Working with memcpy because of alignment constraints
198
                           on Sparc - Gertjan */
199
                        memcpy(&tmp_long, &buf[2], sizeof(long));
200
                        if (tmp_long != intel_long(0x53726448) )
201
                                die("Wrong magic in loader header of 'setup'");
202
                        memcpy(&tmp_int, &buf[6], sizeof(int));
203
                        if (tmp_int < intel_int(0x200))
204
                                die("Wrong version of loader header of 'setup'");
205
                        buf[0x11] = 1; /* LOADED_HIGH */
206
                        tmp_long = intel_long(0x100000);
207
                        memcpy(&buf[0x14], &tmp_long, sizeof(long));  /* code32_start */
208
                }
209
#endif
210
                if (write(1,buf,c)!=c)
211
                        die("Write call failed");
212
#ifdef __BIG_KERNEL__
213
        }
214
#endif
215
        if (c != 0)
216
                die("read-error on 'setup'");
217
        close (id);
218
        setup_sectors = (unsigned char)((i + 511) / 512);
219
        /* for compatibility with LILO */
220
        if (setup_sectors < SETUP_SECTS)
221
                setup_sectors = SETUP_SECTS;
222
        fprintf(stderr,"Setup is %d bytes.\n",i);
223
        for (c=0 ; c<sizeof(buf) ; c++)
224
                buf[c] = '\0';
225
        while (i < setup_sectors * 512) {
226
                c = setup_sectors * 512 - i;
227
                if (c > sizeof(buf))
228
                        c = sizeof(buf);
229
                if (write(1,buf,c) != c)
230
                        die("Write call failed");
231
                i += c;
232
        }
233
 
234
        if ((id=open(argv[3],O_RDONLY,0))<0)
235
                die("Unable to open 'system'");
236
#ifndef __BFD__
237
        if (read(id,buf,GCC_HEADER) != GCC_HEADER)
238
                die("Unable to read header of 'system'");
239
        if (N_MAGIC(*ex) == ZMAGIC) {
240
                GCC_HEADER = N_MAGIC_OFFSET;
241
                lseek(id, GCC_HEADER, SEEK_SET);
242
        } else if (N_MAGIC(*ex) != QMAGIC)
243
                die("Non-GCC header of 'system'");
244
        fprintf(stderr,"System is %d kB (%d kB code, %d kB data and %d kB bss)\n",
245
                (ex->a_text+ex->a_data+ex->a_bss)/1024,
246
                ex->a_text /1024,
247
                ex->a_data /1024,
248
                ex->a_bss  /1024);
249
        sz = N_SYMOFF(*ex) - GCC_HEADER + 4;
250
#else
251
        if (fstat (id, &sb)) {
252
          perror ("fstat");
253
          die ("Unable to stat 'system'");
254
        }
255
        sz = sb.st_size;
256
        fprintf (stderr, "System is %d kB\n", sz/1024);
257
#endif
258
        sys_size = (sz + 15) / 16;
259
        if (sys_size > SYS_SIZE)
260
                die("System is too big");
261
        while (sz > 0) {
262
                int l, n;
263
 
264
                l = sz;
265
                if (l > sizeof(buf))
266
                        l = sizeof(buf);
267
                if ((n=read(id, buf, l)) != l) {
268
                        if (n == -1)
269
                                perror(argv[1]);
270
                        else
271
                                fprintf(stderr, "Unexpected EOF\n");
272
                        die("Can't read 'system'");
273
                }
274
                if (write(1, buf, l) != l)
275
                        die("Write failed");
276
                sz -= l;
277
        }
278
        close(id);
279
        if (lseek(1, 497, 0) == 497) {
280
                if (write(1, &setup_sectors, 1) != 1)
281
                        die("Write of setup sectors failed");
282
        }
283
        if (lseek(1,500,0) == 500) {
284
                buf[0] = (sys_size & 0xff);
285
                buf[1] = ((sys_size >> 8) & 0xff);
286
                if (write(1, buf, 2) != 2)
287
                        die("Write failed");
288
        }
289
        return(0);
290
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.