summaryrefslogtreecommitdiff
path: root/package/aboot/src/netabootwrap.c
blob: aa49b43fd3b8e3ec284ef4c1228640416608023f (plain)
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
#include <sys/types.h>
#include <asm/console.h>
#include "system.h"
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "netwrap.h"
#include "bootloader.h"


char *tfn="netboot.img", *kfn="vmlinux.gz", *ifn=NULL, *barg=NULL;
char *progname;

void print_usage(void )
{
	printf("Following shows options and default values or example value\n");
	printf("%s -t netboot.img -k vmlinux.gz -i initrd.gz -a \"root=/dev/hda1 single\"\n", progname);
	exit(1);
}

void open_file(char *fn, int *fd, int *sz)
{
	struct stat buf;

	*fd = open(fn, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "%s: Cannot open %s\n", progname, fn);
		print_usage();
		exit(1);
	}

	fstat(*fd, &buf);

	if (buf.st_size <= 10*1024) {
		fprintf(stderr, "%s:Is this a right file %s, size = %d\n", progname, fn, (int)buf.st_size);
		print_usage();
		exit(1);
	}

	*sz = buf.st_size;
}

void append_file(int tfd, int sfd)
{
	char buf[4096];
	int  red;

	while ((red=read(sfd, buf, 4096)))
		write(tfd, buf, red);
}

int main(int argc, char **argv)
{
	int tfd=0, kfd=0, ifd=0, ksz=0, isz=0;
	struct header hdr;
	char *stmp;

	progname=argv[0];

	/*
	 * Read switches.
	 */
	for (argc--, argv++; argc > 0; argc--, argv++) {
		if (argv[0][0] != '-')
			break;
		switch (argv[0][1]) {

		case 't':		    /* override target file name */
			if (argv[0][2]) {
				stmp = &(argv[0][2]);
			} else {
				argc--;
				argv++;
				stmp = argv[0];
			}
			if (!stmp) {
				fprintf(stderr,
				    "%s: missing file name for target\n",progname);
				break;
			}
			tfn = stmp;
			break;
		case 'k':			/* override kernel name */
			if (argv[0][2]) {
				stmp = &(argv[0][2]);
			} else {
				argc--;
				argv++;
				stmp = argv[0];
			}
			if (!stmp) {
				fprintf(stderr,
					"%s: missing file name for kernel\n",progname);
				break;
			}
			kfn = stmp;
			break;
		case 'i':			/* override file name for initrd */
			if (argv[0][2]) {
				stmp = &(argv[0][2]);
			} else {
				argc--;
				argv++;
				stmp = argv[0];
			}
			if (!stmp) {
				fprintf(stderr,
					"%s: missing file name for initial RAM-disk\n",progname);
				break;
			}
			ifn = stmp;
			break;
		case 'a':			/* add kernel parameters */
			if (argv[0][2]) {
				stmp = &(argv[0][2]);
			} else {
				argc--;
				argv++;
				stmp = argv[0];
			}
			if (!stmp) {
				fprintf(stderr,
					"%s: No kernel parameters specified\n",progname);
				break;
			}
			barg = stmp;
			break;
		default:
			fprintf(stderr, "%s: unknown switch: -%c\n",
					progname, argv[0][1]);
			print_usage();
			break;

		} /* switch */
	} /* for args */

	open_file(kfn, &kfd, &ksz);

	if (ifn)
		open_file(ifn, &ifd, &isz);

	printf("Target file name is %s\n", tfn);
	unlink(tfn);
	tfd = open(tfn, O_RDWR|O_CREAT, 0644);

	write(tfd, bootloader, sizeof(bootloader));

	hdr.header_size = sizeof(int)*3;
	hdr.kern_size = ksz;
	hdr.ird_size = isz;

	if (barg) printf("With kernel arguments : %s \n", barg);
	else printf("Without kernel argument\n");

	if (barg) {
		if (strlen(barg) >= 200) {
			printf("Kernel argument-list is too long\n");
			exit(1);
		}
		strncpy(hdr.boot_arg, barg, strlen(barg)+1);
		hdr.header_size += strlen(barg)+1;
	}

	lseek(tfd, align_512(sizeof(bootloader)), SEEK_SET);
	write(tfd, &hdr, hdr.header_size);

	printf("Binding kernel %s\n", kfn);
	lseek(tfd, align_512((unsigned long)lseek(tfd, 0, SEEK_CUR)), SEEK_SET);
	append_file(tfd, kfd);

	if (ifn) {
		printf("Binding initrd %s\n", ifn);
		lseek(tfd, align_512((unsigned long)lseek(tfd, 0, SEEK_CUR)), SEEK_SET);
		append_file(tfd, ifd);
	}

	close(tfd);
	printf("Done.\n");
	return 0;
}