[go: up one dir, main page]

File: misc.c

package info (click to toggle)
silo 0.8.5-2.1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,016 kB
  • ctags: 2,061
  • sloc: ansic: 10,060; asm: 2,319; makefile: 351; perl: 74; sh: 3
file content (473 lines) | stat: -rw-r--r-- 12,887 bytes parent folder | download
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* Miscelaneous functions

   Copyright (C) 1996 David Miller
   1996 Pete A. Zaitcev
   1996,1997 Jakub Jelinek

   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
   (at your option) 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.  */

#include "silo.h"
#include <asm/idprom.h>
#include <asm/machines.h>

void fatal (const char *msg)
{
    printf ("\nFatal error: %s\n", msg);
}

char *v0_device (char *imagename)
{
    if (((imagename[0] == 's' && strchr ("dt", imagename[1])) ||
        (imagename[0] == 'x' && strchr ("dy", imagename[1])) ||
        (imagename[0] == 'f' && imagename[1] == 'd') ||
        (imagename[0] == 'l' && imagename[1] == 'e') ||
        (imagename[0] == 'i' && imagename[1] == 'e')) && 
        imagename[2] == '(') {
        return strchr (imagename, ')');
    } else
        return 0;
}

char *seed_part_into_device (char *device, int part)
{
	static char buffer[256];
	
	strcpy (buffer, device);
	if (prom_vers != PROM_V0) {
	    	char *p = strchr (buffer, ':');
	    	
	    	if (!p) {
	    	    p = strchr (buffer, 0);
	    	    *p++ = ':';
	    	} else
	    	    p++;
	    	*p++ = 'a' + part - 1;
	    	*p = 0;
	} else {
	        int i = strlen (device);
	        char *p;
	        
	        if (i >= 4 && buffer[2] == '(' && buffer[i - 1] == ')') {
	        	if (i == 4) {
	        	    strcpy (buffer + 3, "0,0,");
	        	    buffer [7] = '0' + part - 1;
	        	    strcpy (buffer + 8, ")");
	        	} else {
	        	    p = strchr (buffer + 3, ',');
	        	    if (!p) {
	        	    	strcpy (buffer + i - 1, ",0,");
	        	    	buffer [i + 2] = '0' + part - 1;
	        	    	strcpy (buffer + i + 3, ")");
	        	    } else {
	        	    	p = strchr (p + 1, ',');
	        	    	if (!p) {
	        	    	    buffer [i - 1] = ',';
	        	    	    buffer [i] = '0' + part - 1;
	        	    	    strcpy (buffer + i + 1, ")");
	        	    	} else {
	        	    	    *p = '0' - part - 1;
	        	    	    strcpy (p + 1, ")");
	        	    	}
	        	    }
	        	}
	        }
	}
	return buffer;
}

static char barg_buf[1024];
char barg_out[1024];

void set_bootargs (char *params, char *device)
{
    char *q, *r, c;
    char **p;
    int iter, i;

    /*
     * We expect a kernel to extract a command line
     * from our dead body promptly, if the arguments are longer than 90 or so. 
     */
    if (params) {
        q = params;
        r = barg_out;
        /* Remove unnecessary spaces */
        do {
            while (*q == ' ') q++;
            if (!*q) break;
            if (r != barg_out) *r++ = ' ';
            while (*q && *q != ' ') *r++ = *q++;
        } while (*q);
        *r = 0;
    } else
        *barg_out = 0;
    switch (prom_vers) {
    case PROM_V0:
        if (strlen (barg_out) > 100 - 12) {
	    p = (*(romvec->pv_v0bootargs))->argv;
	    p [0] = "silo()";
	    q = barg_out;
	    for (iter = 1; iter < 7; iter++) {
	        while (*q == ' ') q++;
	        if (!*q) {
	            p [iter] = 0;
	            continue;
	        }
	        r = q;
	        while (*r && *r != ' ') r++;
	        if (!p[iter] || strlen (p[iter]) != r - q || strncmp (q, p[iter], r - q)) {
	    	    if (*r) *r++ = 0;
	            p [iter] = q;
	        }
	        q = r;
	    }
	    while (*q == ' ') q++;
	    if (*q)
	        p [7] = q;
	    else
	        p [7] = 0;
	    return;
	} else {
	    q = (*(romvec->pv_v0bootargs))->args;
	    p = (*(romvec->pv_v0bootargs))->argv;
	    p[0] = q;
	    if (!device) {
	    	strcpy (q, "silo()");
	    	q += 7;
	    	strcpy (q, barg_out);
	    } else {
	    	strcpy (q, device);
	    	q = strchr (q, 0);
	    	strcpy (q, barg_out);
	    	r = strchr (q, ' ');
	    	if (!r)
	    	    r = strchr (q, 0);
	    	else {
	    	    *r = 0;
	    	    r++;
	    	}
	    	(*(romvec->pv_v0bootargs))->kernel_file_name = q;
	    	(*(romvec->pv_v0bootargs))->boot_dev[0] = device[0];
	    	(*(romvec->pv_v0bootargs))->boot_dev[1] = device[1];
	    	q = device + 3;
	    	i = 0;	
	    	while (*q != ',' && *q != ')') {
	    	    i = i * 10 + *q - '0';
	    	    q++;
	    	}
	    	if (*q == ',') q++;
	    	(*(romvec->pv_v0bootargs))->boot_dev_ctrl = i;
	    	i = 0;	
	    	while (*q != ',' && *q != ')') {
	    	    i = i * 10 + *q - '0';
	    	    q++;
	    	}
	    	if (*q == ',') q++;
	    	(*(romvec->pv_v0bootargs))->boot_dev_unit = i;
	    	i = 0;	
	    	while (*q != ',' && *q != ')') {
	    	    i = i * 10 + *q - '0';
	    	    q++;
	    	}
	    	(*(romvec->pv_v0bootargs))->dev_partition = i;
	    	q = r;
	    }
	    for (i = 1; i < 7; i++) {
	    	if (*q) {
	    	    r = strchr (q, ' ');
	    	    if (r) {
	    	        *r = 0;
	    	        r++;
	    	    } else
	    	    	r = strchr (q, 0);
	    	    p[i] = q;
	    	    q = r;
	    	} else
	    	    p[i] = 0;
	    }
	    if (*q)
	        p[7] = q;
	    else
	        p[7] = 0;
	    return;
	}
    case PROM_V2:
    case PROM_V3:
    	if (strlen (barg_out) < 128)
    	    strcpy (*romvec->pv_v2bootargs.bootargs, barg_out);
    	else
            *romvec->pv_v2bootargs.bootargs = barg_out;
        if (device)
            strcpy (*romvec->pv_v2bootargs.bootpath, device);
        break;
    case PROM_P1275:
	break;
    }
}

char *get_bootargs (int full)
{
    int iter;
    char *cp, *q;
    char **p;

    switch (prom_vers) {
    case PROM_V0:
        p = (*(romvec->pv_v0bootargs))->argv;
	cp = barg_buf;
	*cp = 0;
	if (p [0]) {
	    for (iter = 0; iter < 8; iter++) {
	    	q = p [iter];
	        if (q) {
	            if (!iter && !full) {
	                q = v0_device (q);
	                
	                if (q && !q[1])
	                    continue;
	                else if (q)
	                    q++;
	                else
	                    q = p [iter];
	            }
	            strcpy (cp, q);
	            cp += strlen (cp);
	            *cp++ = ' ';
	        } else
	            break;
	    }
	    if (cp != barg_buf)
	        cp[-1] = 0;
	}
	break;
    case PROM_V2:
    case PROM_V3:
    	if (!full)
    	    q = barg_buf;
    	else {
            strcpy (barg_buf, *romvec->pv_v2bootargs.bootpath);
            q = strchr (barg_buf, 0);
        }
        if (*romvec->pv_v2bootargs.bootargs) {
            if (full)
                *q++ = ' ';
	    strcpy (q, *romvec->pv_v2bootargs.bootargs);
	} else if (!full)
	    *q = 0;
	break;
    case PROM_P1275:
    	if (!full)
    	    q = barg_buf;
    	else {
    	    iter = prom_getproperty (prom_chosen, "bootpath", barg_buf, 510);
    	    if (iter != -1)
    	        if (iter && !barg_buf [iter - 1])
    	            q = barg_buf + iter - 1;
    	        else
    	            q = barg_buf + iter;
    	    else
    	        q = barg_buf;
    	}
    	iter = prom_getproperty (prom_chosen, "bootargs", full ? q + 1 : q, 512);
    	if (iter == -1 || !iter)
    	    *q = 0;
    	else {
    	    if (full)
    	        *q = ' ';
    	    if (q [iter + 1])
    	    	q [iter + 1] = 0;
    	}
    	break;
    }
    return barg_buf;
}

void show_bootargs ()
{
    printf ("Kernel args: %s\n", get_bootargs (0));
}

unsigned char *find_linux_HdrS (int len)
{
    /* Ugly magic to find HdrS, we dig into first jmp gokernel */
    char *p = (char *) 0x4000 + ((*(unsigned short *) 0x4002) << 2) - 512;
    char *q;

    q = (char *) 0x4008;
    if (*q == 'H' && q[1] == 'd' && q[2] == 'r' && q[3] == 'S')
    	return q;
    if (p >= (char *) 0x4000 + len || p <= (char *) 0x4000)
	return 0;
    for (q = p + 512; p < q; p += 4) {
	if (*p == 'H' && p[1] == 'd' && p[2] == 'r' && p[3] == 'S')
	    return p;
    }
    return 0;
}

static struct idp_struct idprm;

/* Here is the master table of Sun machines which use some implementation
 * of the Sparc CPU and have a meaningful IDPROM machtype value that we
 * know about.  See asm/machines.h for empirical constants.
 */
struct SMM {
    char *name;
    char *package;
    enum arch architecture;
    unsigned char id_machtype;
} Machines[NUM_SUN_MACHINES] = {
/* First, Sun4's */
{ "Sun 4/100 Series", "sun4", sun4, (SM_SUN4 | SM_4_110) },
{ "Sun 4/200 Series", "sun4", sun4, (SM_SUN4 | SM_4_260) },
{ "Sun 4/300 Series", "sun4", sun4, (SM_SUN4 | SM_4_330) },
{ "Sun 4/400 Series", "sun4", sun4, (SM_SUN4 | SM_4_470) },
/* Now, Sun4c's */
{ "SparcStation 1", 	"SUNW,Sun_4_60", sun4c, (SM_SUN4C | SM_4C_SS1) },
{ "SparcStation IPC", 	"SUNW,Sun_4_40", sun4c, (SM_SUN4C | SM_4C_IPC) },
{ "SparcStation 1+", 	"SUNW,Sun_4_65", sun4c, (SM_SUN4C | SM_4C_SS1PLUS) },
{ "SparcStation SLC", 	"SUNW,Sun_4_20", sun4c, (SM_SUN4C | SM_4C_SLC) },
{ "SparcStation 2", 	"SUNW,Sun_4_75", sun4c, (SM_SUN4C | SM_4C_SS2) },
{ "SparcStation ELC", 	"SUNW,Sun_4_25", sun4c, (SM_SUN4C | SM_4C_ELC) },
{ "SparcStation IPX", 	"SUNW,Sun_4_50", sun4c, (SM_SUN4C | SM_4C_IPX) },
/* Finally, early Sun4m's */
{ "SparcSystem 600", 	"SUNW,Sun_4_600", sun4m, (SM_SUN4M | SM_4M_SS60) },
{ "SparcStation 10/20", 	"sun4m", sun4m, (SM_SUN4M | SM_4M_SS50) },
{ "SparcStation 4/5", 	"sun4m", sun4m, (SM_SUN4M | SM_4M_SS40) },
/* One entry for the OBP arch's which are sun4d, sun4e, sun4u and newer sun4m's */
{ "OBP based system", 	"sun4m", sun4m, (SM_SUN4M_OBP | 0x0) } };

char *get_systype(void)
{
    static char system_name[128];
    int i;

    for(i = 0; i < NUM_SUN_MACHINES; i++)
	if(Machines[i].id_machtype == idprm.id_machtype) {
	    if(idprm.id_machtype!=(SM_SUN4M_OBP | 0x0))
		return Machines[i].name;
	    else {
		prom_getproperty(prom_root_node, "banner-name",
			         system_name, sizeof(system_name));
		return system_name;
	    }
	}
    return "Unknown Sparc";
}

char *get_syspackage(void)
{
    static char system_name[128];
    int i;

    *system_name = 0;
    prom_getproperty(prom_root_node, "name",
		     system_name, sizeof(system_name));
    if (*system_name)
	return system_name;
    for(i = 0; i < NUM_SUN_MACHINES; i++)
	if(Machines[i].id_machtype == idprm.id_machtype) {
	    return Machines[i].package;
	}
    return "sun4c";
}

void
get_idprom(void)
{
    prom_getproperty (prom_root_node, "idprom", (char *)&idprm, sizeof (idprm));
}

void print_message (char *msg)
{
    char *p = msg, *q;
    int curly;
    int i;

    for (;;) {
        while (*p && *p != '$') p++;
        if (*p == '$') {
            *p = 0;
            printf ("%s", msg);
            msg = p;
            *p++ = '$';
            curly = (*p == '{');
            if (curly)
        	p++;
            if (!strncmp (p, "ARCH", 4)) {
                switch (get_architecture ()) {
                    case sun4: q = "SUN4"; break;
                    case sun4c: q = "SUN4C"; break;
                    case sun4d: q = "SUN4D"; break;
                    case sun4m: q = "SUN4M"; break;
                    case sun4e: q = "SUN4E"; break;
                    case sun4u: q = "SUN4U"; break;
                }
                printf ("%s", q);
                p += 4;
            } else if (!strncmp (p, "PROM", 4)) {
            	switch (prom_vers) {
            	case PROM_V0: printf ("0"); break;
            	case PROM_V2: printf ("2"); break;
            	case PROM_V3: printf ("3"); break;
            	case PROM_P1275: printf ("IEEE"); break;
            	}
            	p += 4;
            } else if (!strncmp (p, "ETHER", 5)) {
    		for (i = 0; i < 6; i++)
    		    printf ("%x%x%s", ((unsigned char)idprm.id_eaddr[i]) >> 4, idprm.id_eaddr[i] & 0xf, i == 5 ? "" : ":");
    		p += 5;
            } else if (!strncmp (p, "SYSTYPE", 7)) {
                printf ("%s", get_systype ());
                p += 7;
            } else continue;
            if (curly && *p == '}') p++;
            msg = p;
        } else {
            printf ("%s", msg);
            break;
        }
    }
}

enum arch get_architecture ()
{
    char *buffer = "sun4c    ";
    int i;
    
    if (prom_vers == PROM_P1275) return sun4u;
    i = prom_getproperty (prom_root_node, "compatability", buffer, 8);

    if (!i || i == -1)
	i = prom_getproperty (prom_root_node, "compatible", buffer, 8);

    switch (buffer[4]) {
    case 'c':
	return sun4c;
    case 'm':
	return sun4m;
    case 'd':
	return sun4d;
    case 'e':
	return sun4e;
    case 'u':
	return sun4u;
    default:
    	for(i = 0; i < NUM_SUN_MACHINES; i++)
	    if(Machines[i].id_machtype == idprm.id_machtype)
	    	return Machines[i].architecture;
	return sununknown;
    }
}