• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/src/linux/linux-2.6/arch/cris/arch-v10/boot/tools/
1/*
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/a.out.h>
34#include <errno.h>
35
36#define MINIX_HEADER 32
37
38#define N_MAGIC_OFFSET 1024
39#ifndef __BFD__
40static int GCC_HEADER = sizeof(struct exec);
41#endif
42
43#ifdef __BIG_KERNEL__
44#define SYS_SIZE 0xffff
45#else
46#define SYS_SIZE DEF_SYSSIZE
47#endif
48
49#define DEFAULT_MAJOR_ROOT 0
50#define DEFAULT_MINOR_ROOT 0
51
52/* max nr of sectors of setup: don't change unless you also change
53 * bootsect etc */
54#define SETUP_SECTS 4
55
56#define STRINGIFY(x) #x
57
58typedef union {
59	int i;
60	long l;
61	short s[2];
62	char b[4];
63} conv;
64
65long intel_long(long l)
66{
67	conv t;
68
69	t.b[0] = l & 0xff; l >>= 8;
70	t.b[1] = l & 0xff; l >>= 8;
71	t.b[2] = l & 0xff; l >>= 8;
72	t.b[3] = l & 0xff; l >>= 8;
73	return t.l;
74}
75
76int intel_int(int i)
77{
78	conv t;
79
80	t.b[0] = i & 0xff; i >>= 8;
81        t.b[1] = i & 0xff; i >>= 8;
82        t.b[2] = i & 0xff; i >>= 8;
83        t.b[3] = i & 0xff; i >>= 8;
84        return t.i;
85}
86
87short intel_short(short l)
88{
89	conv t;
90
91	t.b[0] = l & 0xff; l >>= 8;
92	t.b[1] = l & 0xff; l >>= 8;
93	return t.s[0];
94}
95
96void die(const char * str)
97{
98	fprintf(stderr,"%s\n",str);
99	exit(1);
100}
101
102void usage(void)
103{
104	die("Usage: build bootsect setup system [rootdev] [> image]");
105}
106
107int main(int argc, char ** argv)
108{
109	int i,c,id,sz,tmp_int;
110	unsigned long sys_size, tmp_long;
111	char buf[1024];
112#ifndef __BFD__
113	struct exec *ex = (struct exec *)buf;
114#endif
115	char major_root, minor_root;
116	struct stat sb;
117	unsigned char setup_sectors;
118
119	if ((argc < 4) || (argc > 5))
120		usage();
121	if (argc > 4) {
122		if (!strcmp(argv[4], "CURRENT")) {
123			if (stat("/", &sb)) {
124				perror("/");
125				die("Couldn't stat /");
126			}
127			major_root = major(sb.st_dev);
128			minor_root = minor(sb.st_dev);
129		} else if (strcmp(argv[4], "FLOPPY")) {
130			if (stat(argv[4], &sb)) {
131				perror(argv[4]);
132				die("Couldn't stat root device.");
133			}
134			major_root = major(sb.st_rdev);
135			minor_root = minor(sb.st_rdev);
136		} else {
137			major_root = 0;
138			minor_root = 0;
139		}
140	} else {
141		major_root = DEFAULT_MAJOR_ROOT;
142		minor_root = DEFAULT_MINOR_ROOT;
143	}
144	fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root);
145	for (i=0;i<sizeof buf; i++) buf[i]=0;
146	if ((id=open(argv[1],O_RDONLY,0))<0)
147		die("Unable to open 'boot'");
148	if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
149		die("Unable to read header of 'boot'");
150	if (((long *) buf)[0]!=intel_long(0x04100301))
151		die("Non-Minix header of 'boot'");
152	if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
153		die("Non-Minix header of 'boot'");
154	if (((long *) buf)[3] != 0)
155		die("Illegal data segment in 'boot'");
156	if (((long *) buf)[4] != 0)
157		die("Illegal bss in 'boot'");
158	if (((long *) buf)[5] != 0)
159		die("Non-Minix header of 'boot'");
160	if (((long *) buf)[7] != 0)
161		die("Illegal symbol table in 'boot'");
162	i=read(id,buf,sizeof buf);
163	fprintf(stderr,"Boot sector %d bytes.\n",i);
164	if (i != 512)
165		die("Boot block must be exactly 512 bytes");
166	if ((*(unsigned short *)(buf+510)) != (unsigned short)intel_short(0xAA55))
167		die("Boot block hasn't got boot flag (0xAA55)");
168	buf[508] = (char) minor_root;
169	buf[509] = (char) major_root;
170	i=write(1,buf,512);
171	if (i!=512)
172		die("Write call failed");
173	close (id);
174
175	if ((id=open(argv[2],O_RDONLY,0))<0)
176		die("Unable to open 'setup'");
177	if (read(id,buf,MINIX_HEADER) != MINIX_HEADER)
178		die("Unable to read header of 'setup'");
179	if (((long *) buf)[0]!=intel_long(0x04100301))
180		die("Non-Minix header of 'setup'");
181	if (((long *) buf)[1]!=intel_long(MINIX_HEADER))
182		die("Non-Minix header of 'setup'");
183	if (((long *) buf)[3] != 0)
184		die("Illegal data segment in 'setup'");
185	if (((long *) buf)[4] != 0)
186		die("Illegal bss in 'setup'");
187	if (((long *) buf)[5] != 0)
188		die("Non-Minix header of 'setup'");
189	if (((long *) buf)[7] != 0)
190		die("Illegal symbol table in 'setup'");
191	for (i=0 ; (c=read(id,buf,sizeof buf))>0 ; i+=c )
192#ifdef __BIG_KERNEL__
193	{
194		if (!i) {
195			/* Working with memcpy because of alignment constraints
196			   on Sparc - Gertjan */
197			memcpy(&tmp_long, &buf[2], sizeof(long));
198			if (tmp_long != intel_long(0x53726448) )
199				die("Wrong magic in loader header of 'setup'");
200			memcpy(&tmp_int, &buf[6], sizeof(int));
201			if (tmp_int < intel_int(0x200))
202				die("Wrong version of loader header of 'setup'");
203			buf[0x11] = 1; /* LOADED_HIGH */
204			tmp_long = intel_long(0x100000);
205			memcpy(&buf[0x14], &tmp_long, sizeof(long));  /* code32_start */
206		}
207#endif
208		if (write(1,buf,c)!=c)
209			die("Write call failed");
210#ifdef __BIG_KERNEL__
211	}
212#endif
213	if (c != 0)
214		die("read-error on 'setup'");
215	close (id);
216	setup_sectors = (unsigned char)((i + 511) / 512);
217	/* for compatibility with LILO */
218	if (setup_sectors < SETUP_SECTS)
219		setup_sectors = SETUP_SECTS;
220	fprintf(stderr,"Setup is %d bytes.\n",i);
221	for (c=0 ; c<sizeof(buf) ; c++)
222		buf[c] = '\0';
223	while (i < setup_sectors * 512) {
224		c = setup_sectors * 512 - i;
225		if (c > sizeof(buf))
226			c = sizeof(buf);
227		if (write(1,buf,c) != c)
228			die("Write call failed");
229		i += c;
230	}
231
232	if ((id=open(argv[3],O_RDONLY,0))<0)
233		die("Unable to open 'system'");
234#ifndef __BFD__
235	if (read(id,buf,GCC_HEADER) != GCC_HEADER)
236		die("Unable to read header of 'system'");
237	if (N_MAGIC(*ex) == ZMAGIC) {
238		GCC_HEADER = N_MAGIC_OFFSET;
239		lseek(id, GCC_HEADER, SEEK_SET);
240	} else if (N_MAGIC(*ex) != QMAGIC)
241		die("Non-GCC header of 'system'");
242	fprintf(stderr,"System is %d kB (%d kB code, %d kB data and %d kB bss)\n",
243		(ex->a_text+ex->a_data+ex->a_bss)/1024,
244		ex->a_text /1024,
245		ex->a_data /1024,
246		ex->a_bss  /1024);
247	sz = N_SYMOFF(*ex) - GCC_HEADER + 4;
248#else
249	if (fstat (id, &sb)) {
250	  perror ("fstat");
251	  die ("Unable to stat 'system'");
252	}
253	sz = sb.st_size;
254	fprintf (stderr, "System is %d kB\n", sz/1024);
255#endif
256	sys_size = (sz + 15) / 16;
257	if (sys_size > SYS_SIZE)
258		die("System is too big");
259	while (sz > 0) {
260		int l, n;
261
262		l = sz;
263		if (l > sizeof(buf))
264			l = sizeof(buf);
265		if ((n=read(id, buf, l)) != l) {
266			if (n == -1)
267				perror(argv[1]);
268			else
269				fprintf(stderr, "Unexpected EOF\n");
270			die("Can't read 'system'");
271		}
272		if (write(1, buf, l) != l)
273			die("Write failed");
274		sz -= l;
275	}
276	close(id);
277	if (lseek(1, 497, 0) == 497) {
278		if (write(1, &setup_sectors, 1) != 1)
279			die("Write of setup sectors failed");
280	}
281	if (lseek(1,500,0) == 500) {
282		buf[0] = (sys_size & 0xff);
283		buf[1] = ((sys_size >> 8) & 0xff);
284		if (write(1, buf, 2) != 2)
285			die("Write failed");
286	}
287	return(0);
288}
289