summaryrefslogtreecommitdiffstats
path: root/utils/mk-s390-cdboot.c
blob: 45e5cf8456da6b754302dec5d3ee255e4c3247c0 (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
/*
 * mk-s390-cdboot -- creates one big image using a kernel, a ramdisk and
 *                     a parmfile
 *
 *
 * 2003-07-24 Volker Sameske <sameske@de.ibm.com>
 *
 * compile with:
 *     gcc -Wall -o mk-s390-cdboot mk-s390-cdboot.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <stdarg.h>

#define BUFFER_LEN 1024
#define INITRD_START 0x0000000000800000LL
#define START_PSW_ADDRESS 0x80010000
#define PARAMETER_BUFFER_LEN 80

static struct option getopt_long_options[]=
{
        { "image",       1, 0, 'i'},
        { "ramdisk",     1, 0, 'r'},
        { "parmfile",    1, 0, 'p'},
        { "outfile",     1, 0, 'o'},
        { "help",        0, 0, 'h'},
        {0, 0, 0, 0}
};


static void usage(char *cmd)
{
        printf("%s [-h] [-v] -i <kernel> -r <ramdisk> -p <parmfile> -o <outfile>\n", cmd);
}


int main (int argc, char **argv)
{
	char *cmd = basename(argv[0]);
	FILE *fd1;
	FILE *fd2;
	FILE *fd3;
	FILE *fd4;
	char buffer[BUFFER_LEN];
	int rc, oc, index;
	unsigned long long initrd_start = INITRD_START;
	unsigned long long initrd_size;
	char image[PARAMETER_BUFFER_LEN];
	char ramdisk[PARAMETER_BUFFER_LEN];
	char parmfile[PARAMETER_BUFFER_LEN];
	char outfile[PARAMETER_BUFFER_LEN];
	int image_specified    = 0;
	int ramdisk_specified  = 0;
	int parmfile_specified = 0;
	int outfile_specified  = 0;
	int start_psw_address  = START_PSW_ADDRESS;

        opterr=0;
        while (1)
        {
                oc = getopt_long(argc, argv, "i:r:p:o:h?", getopt_long_options, &index);
                if (oc==-1) break;

                switch (oc)
                {
                case '?':
                case 'h':
                        usage(cmd);
                        exit(0);
                case 'i':
			strcpy(image, optarg);
                        image_specified = 1;
                        break;
		case 'r':
                        strcpy(ramdisk, optarg);
                        ramdisk_specified = 1;
                        break;
		case 'p':
                        strcpy(parmfile, optarg);
                        parmfile_specified = 1;
                        break;
		case 'o':
                        strcpy(outfile, optarg);
                        outfile_specified = 1;
                        break;
		default:
                        usage(cmd);
                        exit(0);
                }
        }

	if (!image_specified || !ramdisk_specified ||
	    !parmfile_specified || !outfile_specified) {
		usage(cmd);
		exit(0);
	}

	printf("Creating bootable CD-ROM image...\n");
        printf("kernel is  : %s\n", image);
        printf("ramdisk is : %s\n", ramdisk);
        printf("parmfile is: %s\n", parmfile);
        printf("outfile is : %s\n", outfile);

	fd1 = fopen(outfile, "w");
	fd2 = fopen(image, "r");
	fd3 = fopen(ramdisk, "r");
	fd4 = fopen(parmfile, "r");

	printf("writing kernel...\n");
	while (1) {
		rc = fread(buffer, BUFFER_LEN, 1, fd2);
		fwrite(buffer, BUFFER_LEN, 1, fd1);
		if (rc == 0) break;
	}

	printf("writing initrd...\n");
	fseek(fd1, initrd_start, SEEK_SET);
	while (1) {
		rc = fread(buffer, BUFFER_LEN, 1, fd3);
		fwrite(buffer, BUFFER_LEN, 1, fd1);
		if (rc == 0) break;
	}

	fseek(fd3, 0 ,SEEK_END);
	initrd_size = ftell(fd3);

	printf("changing start PSW address to 0x%08x...\n", start_psw_address);
	fseek(fd1, 0x4, SEEK_SET);
	fwrite(&start_psw_address, 4, 1, fd1);

	printf("writing initrd address and size...\n");
	printf("INITRD start: 0x%016llx\n",  initrd_start);
	printf("INITRD size : 0x%016llx\n", initrd_size);

	fseek(fd1, 0x10408, SEEK_SET);
	fwrite(&initrd_start, 8, 1, fd1);
	fseek(fd1, 0x10410, SEEK_SET);
	fwrite(&initrd_size, 8, 1, fd1);

	printf("writing parmfile...\n");
	fseek(fd1, 0x10480, SEEK_SET);
	while (1) {
		rc = fread(buffer, 1, 1, fd4);
		fwrite(buffer, 1, 1, fd1);
		if (rc == 0) break;
	}

	fclose(fd1);
	fclose(fd2);
	fclose(fd3);
	fclose(fd4);
	return 0;
}