groklilo.c 7.06 KB
Newer Older
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
/*
 * Code to grok a LILO bootblock and change the default command line string
 */

/*
 * XXX hack to recognize compilation in the boot loader as opposed to
 * as a standalone application.
 */
#ifdef BOOT_FORTH
#define INBOOTLOADER
#endif

#include <string.h>

#ifdef INBOOTLOADER
#include <stand.h>
#include "emuboot.h"

#ifdef DEBUG
#define debug 1
#else
#define debug 0
#endif
#else
#include <sys/types.h>
#include <stdio.h>

static int debug;
#endif

void *load_partition_bblock(int partition);
int load_sector(int sectno, void *buf);
int save_sector(int sectno, void *buf);

/*
 * These need to be consistant with LILO
 */
#define MAX_BOOT2_SECT	10
#define MAX_IMAGE_DESC	19
#define MAX_MAP_SECT	101

/*
 * Sector addresses.
 * These are the absolute values that must be relocated when moving
 * a bootable partition.
 */
typedef struct sectaddr {
	uint8_t sector;
	uint8_t track;
	uint8_t device;		/* + flags, see below */
	uint8_t head;
	uint8_t nsect;
} sectaddr_t;

/* flags encoded in device */
#define HARD_DISK	0x80	/* not a floppy */
#define LINEAR_ADDR	0x40	/* mark linear address */
#define LBA32_ADDR	0x20    /* mark lba 32-bit address */
#define LBA32_NOCOUNT   0x10    /* mark address with count absent */
#define DEVFLAGS	0xF0

struct bb1 {
	uint8_t jumpinst[6];
	char sig[4];		/* LILO */
	uint16_t stage;		/* 1 -- stage1 loader */
	uint16_t version;
	uint16_t timeout;
	uint16_t delay;
	uint8_t port, portparams;
	uint32_t timestamp;
	sectaddr_t idesc[2];	/* image descriptors */
	sectaddr_t cmdline;	/* command line (max 1 sector?) */
	uint8_t prompt;
	uint16_t msglen;
	sectaddr_t msg;		/* "initial greeting message" */
	sectaddr_t keytab;	/* "keyboard translation table" */
	sectaddr_t boot2[MAX_BOOT2_SECT+1]; /* 2nd stage boot sectors */
};

union bblock {
	struct bb1 bb1;
	char data[1*512];
};

struct cmdlineblock {
	u_int16_t magic;
	char cmdline[510];
};
#define CMDLINE_MAGIC	0xF4F2

static uint32_t
getsector(sectaddr_t *sect)
{
	int flags = (sect->device & DEVFLAGS) & ~HARD_DISK;
	uint32_t sector = 0;

	if (sect->sector == 0 && sect->track == 0 && sect->device == 0 &&
	    sect->head == 0 && sect->nsect == 0)
		return 0;

	if (flags == 0) {
		printf("groklilo: no can do CHS addresses!\n");
		return ~0;
	}

	if (flags & LINEAR_ADDR) {
		sector |= sect->head << 16;
		sector |= sect->track << 8;
		sector |= sect->sector;
	} else {
		if (flags & LBA32_NOCOUNT)
			sector |= sect->nsect << 24;
		sector |= sect->head << 16;
		sector |= sect->track << 8;
		sector |= sect->sector;
	}

	return sector;
}

int
set_lilo_cmdline(int partition, char *cmdline)
{
	struct bb1 *bb;
	struct cmdlineblock clblock;
	uint32_t cbsect;

	printf("Setting LILO cmdline in partition %d to `%s'\n",
	       partition, cmdline);

	if (partition < 1 || partition > 4) {
		if (debug)
			printf("%d: invalid partition number\n", partition);
		return 1;
	}

	bb = load_partition_bblock(partition);
	if (bb == NULL)
		return 1;

	if (strncmp(bb->sig, "LILO", 4) != 0) {
		if (debug)
			printf("%d: no LILO bootblock in partition\n",
			       partition);
		return 1;
	}

	if (bb->stage != 1) {
		if (debug)
			printf("%d: can only handle LILO stage1 bootblock\n",
			       partition);
		return 1;
	}

	cbsect = getsector(&bb->cmdline);
	if (cbsect == ~0) {
		if (debug)
			printf("%d: error parsing LILO cmdline sector\n",
			       partition);
		return 1;
	}

	if (load_sector(cbsect, &clblock)) {
		if (debug)
			printf("%d: could not read LILO cmdline sector %d\n",
			       partition, cbsect);
		return 1;
	}

	if (debug) {
		printf("%d: old command line was: %s\n",
		       partition, clblock.magic == CMDLINE_MAGIC ?
		       clblock.cmdline : "INVALID");
	}

	if (cmdline[0] == 0) {
		clblock.magic = 0x6d6b; /* XXX what lilo does */
	} else {
		strncpy(clblock.cmdline, cmdline, sizeof(clblock.cmdline));
		clblock.magic = CMDLINE_MAGIC;
	}

	if (save_sector(cbsect, &clblock)) {
		if (debug)
			printf("%d: could not write LILO cmdline sector %d\n",
			       partition, cbsect);
		return 1;
	}

	return 0;
}

#ifndef INBOOTLOADER

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

static int devfd;
static int nogo;

static char *usagestr = 
 "usage: %s <options> partition devicefile\n"
 " -d              Turn on debugging.\n"
 " -c 'str'        Set the LILO command line\n"
 " -n              Do not actually perform the operation\n"
 " partition       The DOS partition number where LILO resides (1-4)\n"
 " device          The *whole* disk device special file (ie: /dev/ad0)\n";

static void
usage(char *progname)
{
	fprintf(stderr, usagestr, progname);
	exit(1);
}

int
main(int argc, char **argv)
{
	int ch, part, rv = 0;
	char *cmdline = 0, *progname;

	progname = argv[0];
	while ((ch = getopt(argc, argv, "c:dn")) != -1)
		switch(ch) {
		case 'd':
			debug++;
			break;
		case 'c':
			cmdline = optarg;
			break;
		case 'n':
			nogo++;
			break;
		case '?':
		default:
			usage(progname);
		}
	argc -= optind;
	argv += optind;

	if (argc != 2)
		usage(progname);

	part = atoi(argv[0]);
	if (part < 1 || part > 4)
		usage(progname);

	if ((devfd = open(argv[1], nogo ? O_RDONLY : O_RDWR)) < 0) {
		perror("opening device");
		exit(1);
	}

	if (cmdline != 0) {
		rv = set_lilo_cmdline(part, cmdline);
		if (rv) {
			fprintf(stderr, "%s: command failed on part %d, "
				"no change made\n",
				argv[1], part);
		}
	}

	exit(rv);
}

struct bios_mbr {
	char bootcode[446];
	char partinfo[64];
	unsigned short magic;
};

/* magic */
#define BIOS_MAGIC	0xaa55

struct bios_partition_info {
        unsigned char bootid;
        unsigned char beghead;
        unsigned char begsect;
        unsigned char begcyl; 
        unsigned char sysid;
        unsigned char endhead;
        unsigned char endsect;
        unsigned char endcyl;
        unsigned long offset;
        unsigned long numsectors;
};

static struct bios_mbr diskmbr;
static char partbr[512];

static int
check_mbr(void)
{
	struct bios_mbr *mbr;
	struct bios_partition_info *entry;
	int i;

	mbr = &diskmbr;
	if (mbr->magic != BIOS_MAGIC)
		return -1;

	entry = (struct bios_partition_info *) mbr->partinfo;
	for (i = 0; i < 4; i++, entry++) {
		if (entry->numsectors == 0)
			continue;

		/* XXX sanity check fields */
	}

	return 0;
}

int
load_sector(int sectno, void *buf)
{
	if (lseek(devfd, (off_t)sectno * 512, SEEK_SET) < 0 ||
	    read(devfd, buf, 512) < 512)
		return errno;
	return 0;
}

int
save_sector(int sectno, void *buf)
{
	if (nogo) {
		fprintf(stderr, "Would write back sector %d\n", sectno);
		return 0;
	}
	if (lseek(devfd, (off_t)sectno * 512, SEEK_SET) < 0 ||
	    write(devfd, buf, 512) < 512)
		return errno;
	return 0;
}

static void
load_mbr(void)
{
	if (load_sector(0, &diskmbr) == 0 && check_mbr() == 0)
		return;

	fprintf(stderr, "Could not load/verify MBR\n");
	exit(-1);
}

void *
load_partition_bblock(int pnum)
{
	struct bios_partition_info *entry;

	load_mbr();

	pnum--;
	entry = ((struct bios_partition_info *)diskmbr.partinfo) + pnum;
	if (load_sector((int)entry->offset, partbr) != 0 ||
	    ((struct bios_mbr *)partbr)->magic != BIOS_MAGIC) {
		printf("Could not load/verify partition %d boot record\n",
		       pnum);
		return 0;
	}

	return partbr;
}

#endif