File: main.c

package info (click to toggle)
netboot 0.8.1-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,728 kB
  • ctags: 4,740
  • sloc: ansic: 15,152; asm: 11,623; yacc: 2,248; makefile: 1,110; pascal: 1,108; lex: 748; sh: 233
file content (404 lines) | stat: -rw-r--r-- 10,352 bytes parent folder | download | duplicates (3)
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
404
/*
 * main.c  -  Utility program to create a bootrom image
 *
 * Copyright (C) 1997,1998 Gero Kuhlmann <gero@gkminix.han.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define NO_BINARY 1	/* No need to include binary support here */
#include "common.h"
#include "nblib.h"
#include "makerom.h"



/*
 * Path name definitions
 */
#define KERNELDIR	"//binaries"
#define LOADERDIR	"//binaries"
#define NETDRVDIR	"//netdrvr"
#define UTILSDIR	"//utils"



/*
 * Global variables
 */
char *kerneldir = NULL;		/* name of directory with kernel binaries */
char *loaderdir = NULL;		/* name of directory with loader binaries */
char *netdrvdir = NULL;		/* name of directory with network drivers */
char *utilsdir = NULL;		/* name of utilities directory */
struct desclist *kerneldesc = NULL;	/* list of kernel descriptions */
struct desclist *netdrvdesc = NULL;	/* list of network driver descriptions */



/*
 * Private variables
 */
static char *batchname = NULL;		/* name of system to batch process */
static int accesserror = 0;		/* non-zero if unable to access a file */



/*
 * Command line options
 */
static struct cmdopt opts[] = {
	{ "batch-sys", 'b', strval, {(char **)&batchname}, NULL,
	  "name of system to process", "SYSTEM"				},
	{ NULL, 0, noval, {NULL}, NULL, NULL, NULL			}
};



/*
 * Definition of makerom:general section in configuration file
 */
static struct paramdef general_params[] = {
	{ "kerneldir",	par_string,	NULL,	{&kerneldir}},
	{ "loaderdir",	par_string,	NULL,	{&loaderdir}},
	{ "netdrvdir",	par_string,	NULL,	{&netdrvdir}},
	{ "utilsdir",	par_string,	NULL,	{&utilsdir}},
	{ NULL,		par_null,	NULL,	{NULL}}
};



/*
 * Define kernel parameters in configuration file
 */
static struct {
	char *description;
	char *filename;
	char *flags;
} kernel_vars = {NULL, NULL, NULL};

static struct paramdef kernel_params[] = {
  { "description",	par_string,	NULL,	{&kernel_vars.description}},
  { "file",		par_string,	NULL,	{&kernel_vars.filename}},
  { "flags",		par_string,	NULL,	{&kernel_vars.flags}},
  { NULL,		par_null,	NULL,	{NULL}}
};



/*
 * Define network driver parameters in configuration file
 */
char *drtype[] = {
  "dos program", "packet driver", "ndis driver", "linux driver", NULL
};

static struct {
	char *description;
	char *filename;
	int   drivertype;
	char *parameters;
	char *cmdline;
} netdrv_vars = {NULL, NULL, 0, NULL, NULL};

static struct paramdef netdrv_params[] = {
  { "description",	par_string,	NULL,	{&netdrv_vars.description}},
  { "file",		par_string,	NULL,	{&netdrv_vars.filename}},
  { "type",		par_string,	drtype,	{(char **)&netdrv_vars.drivertype}},
  { "parameters",	par_string,	NULL,	{&netdrv_vars.parameters}},
  { "cmdline",		par_string,	NULL,	{&netdrv_vars.cmdline}},
  { NULL,		par_null,	NULL,	{NULL}}
};



/*
 * List of sections to read from configuration file at startup
 */
static char *handle_kernel_sect __P((char *sectname, struct sectdef *cursect));
static char *handle_netdrv_sect __P((char *sectname, struct sectdef *cursect));

static struct sectdef sects[] = {
	{ "makerom:general",	general_params,	NULL,	NULL },
	{ "makerom:kernel:*",	kernel_params,	NULL,	&handle_kernel_sect },
	{ "makerom:netdrv:*",	netdrv_params,	NULL,	&handle_netdrv_sect },
	{ NULL,			NULL,		NULL,	NULL}
};



/*
 * List of recognized options for binary files
 */
struct optlist {
	char *optname;
	int   optval;
};

static struct optlist netoptlist[] = {
	{ "HW_IRQ",	HW_IRQ },
	{ "IO_ADDR",	IO_ADDR },
	{ "BASE_MEM",	BASE_MEM },
	{ "AUI_TYPE",	AUI_TYPE },
	{ "DMA_NUM",	DMA_NUM },
	{ NULL,		0 }
};

static struct optlist kernoptlist[] = {
	{ "MINIMAL",	K_MINIMAL },
	{ "X86",	K_X86 },
	{ NULL,		0 }
};



/*
 * Generate an option value from an option string
 */
static int getoption(optstr, optlist)
char *optstr;
struct optlist *optlist;
{
  char *cp;
  int i, optval = 0;

  cp = strtok(optstr, ",");
  while (cp != NULL) {
	for (i = 0; optlist[i].optname != NULL; i++)
		if (!strcmp(optlist[i].optname, cp))
			break;
	if (optlist[i].optname == NULL)
		return(-1);
	optval |= optlist[i].optval;
	cp = strtok(NULL, ",");
  }
  return(optval);
}



/*
 * Handle processing of parameters for a kernel description section. This
 * routine gets called after a kernel description section has been read.
 */
static char *handle_kernel_sect(sectname, cursect)
char *sectname;
struct sectdef *cursect;
{
  struct desclist *dp;
  char *cp;
  int optval = 0;

  /* Analyze kernel parameters */
  if (kernel_vars.flags != NULL) {
	if ((optval = getoption(kernel_vars.flags, kernoptlist)) < 0)
		return("invalid kernel flag");
	free(kernel_vars.flags);
  }
  if (kernel_vars.filename == NULL)
	return("missing kernel file name");

  /* Add new item to kernel description list */
  dp = nbmalloc(sizeof(struct desclist));
  dp->filename = kernel_vars.filename;
  dp->descript = kernel_vars.description;
  dp->drivertype = TYPE_NONE;
  dp->cmdline = NULL;
  dp->options = optval;
  dp->progname = NULL;
  if ((cp = strrchr(sectname, ':')) == NULL)
	copystr(&(dp->progname), sectname);
  else
	copystr(&(dp->progname), (cp + 1));
  dp->next = kerneldesc;
  kerneldesc = dp;

  /* Clear out temporary space for next round */
  memset(&kernel_vars, 0, sizeof(kernel_vars));
  return(NULL);
}



/*
 * Handle processing of parameters for a network driver description section.
 * This routine gets called after a driver description section has been read.
 */
static char *handle_netdrv_sect(sectname, cursect)
char *sectname;
struct sectdef *cursect;
{
  struct desclist *dp;
  char *cp;
  int optval = 0;

  /* Analyze network driver parameters */
  if (netdrv_vars.parameters != NULL) {
	if ((optval = getoption(netdrv_vars.parameters, netoptlist)) < 0)
		return("invalid network driver flag");
	free(netdrv_vars.parameters);
  }
  if (!netdrv_vars.drivertype)
	return("missing network driver type");
  if (netdrv_vars.filename == NULL)
	return("missing network driver file name");
  if (netdrv_vars.cmdline == NULL)
	return("missing network driver command line");

  /* Add new item to kernel description list */
  dp = nbmalloc(sizeof(struct desclist));
  dp->filename = netdrv_vars.filename;
  dp->descript = netdrv_vars.description;
  dp->drivertype = netdrv_vars.drivertype;
  dp->cmdline = netdrv_vars.cmdline;
  dp->options = optval;
  dp->progname = NULL;
  if ((cp = strrchr(sectname, ':')) == NULL)
	copystr(&(dp->progname), sectname);
  else
	copystr(&(dp->progname), (cp + 1));
  dp->next = netdrvdesc;
  netdrvdesc = dp;

  /* Clear out temporary space for next round */
  memset(&netdrv_vars, 0, sizeof(netdrv_vars));
  return(NULL);
}



/*
 * Check that we have read access to the specified filed
 */
static void checkaccess(fname)
char *fname;
{
  if (access(fname, R_OK) < 0) {
	fprintf(stderr, "%s: unable to access file %s\n", progname, fname);
	accesserror++;
  }
}



/*
 * Print boot definition record for debugging purposes
 */
static void printbootdef(bp)
struct bootdef *bp;
{
  int i;

  if (bp->name != NULL)
	printf("Bootrom building definitions for %s\n", bp->name);
  else
	printf("Bootrom building definitions\n");
  printf("Kernel file        = %s (int %dh)\n", bp->kernelname,
			bp->use_int18 ? 18 : 19);

  for (i = 0; i < MAXLOADERS; i++)
	if (bp->loadernames[i] != NULL) {
		printf("Loader binary %d    = %s\n", i, bp->loadernames[i]);
		printf("Output file %d      = %s\n", i, bp->outnames[i]);
	}
  printf("\n");

  for (i = 0; i < MAXDRIVERS; i++)
	if (bp->drivernames[i] != NULL) {
		printf("Driver %d program   = %s\n", i, bp->drivernames[i]);
		printf("Driver %d arguments = %s\n", i, bp->driverargs[i]);
	}
  printf("\n");
}



/*
 * Main program
 */
void main(argc, argv)
int argc;
char **argv;
{
  struct bootdef *bp;
  char *tmpfname;
  int i, tmpfile;

  /* Parse options and read configuration file */
  nbsetup(argc, argv, opts, sects);
  setpath(&kerneldir, KERNELDIR);
  setpath(&loaderdir, LOADERDIR);
  setpath(&netdrvdir, NETDRVDIR);
  setpath(&utilsdir, UTILSDIR);

  if (verbose > 0) {
	printf("Bootrom configuration program, " VERSION "\n" COPYRIGHT "\n");
	printf("Database file           = %s\n", dbname);
	printf("Kernel directory        = %s\n", kerneldir);
	printf("Loader directory        = %s\n", loaderdir);
	printf("Packet driver directory = %s\n", netdrvdir);
	printf("Utilities directory     = %s\n\n", utilsdir);
  }

  /* Get bootrom specification either from the user or the database */
  if (batchname != NULL) {
	bp = getdb(batchname);
  } else {
	if (!verbose)
		printf("\nBootrom configuration program, "
					VERSION "\n" COPYRIGHT "\n\n");
	bp = getuser();
  }

  /* Expand filenames */
  setpath(&(bp->kernelname), NULL);
  for (i = 0; i < MAXLOADERS; i++)
	setpath(&(bp->loadernames[i]), NULL);
  for (i = 0; i < MAXDRIVERS; i++)
	setpath(&(bp->drivernames[i]), NULL);
  if (verbose > 1)
	printbootdef(bp);

  /* Check that all input files are readable */
  checkaccess(bp->kernelname);
  for (i = 0; i < bp->loadernum; i++)
	checkaccess(bp->loadernames[i]);
  for (i = 0; i < bp->drivernum; i++)
	checkaccess(bp->drivernames[i]);
  if (accesserror > 0)
	exit(EXIT_ACCESS);

  /* Open temporary file and call the different passes successively */
  if ((tmpfname = tempnam(NULL, "mkrom")) == NULL) {
	fprintf(stderr, "%s: unable to generate temporary file name\n",
								progname);
	exit(EXIT_TEMPNAME);
  }
  if ((tmpfile = open(tmpfname, O_CREAT | O_RDWR | O_TRUNC | O_BINARY,
								0644)) < 0) {
	fprintf(stderr, "%s: unable to open temporary file %s\n",
							progname, tmpfname);
	exit(EXIT_CREATE);
  }
  if (verbose < 2)
	unlink(tmpfname);

  pass1(bp, tmpfile);
  for (i = 0; i < bp->loadernum; i++)
	pass2(bp, tmpfile, i);

  close(tmpfile);
  exit(EXIT_SUCCESS);
}