~ubuntu-branches/ubuntu/hardy/silo/hardy-proposed

« back to all changes in this revision

Viewing changes to silo/prom.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio M. Di Nitto
  • Date: 2007-10-25 09:28:08 UTC
  • mfrom: (15.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071025092808-1yhj12t7s4zqsfu5
Tags: 1.4.13a+git20070930-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build with -fno-stack-protector.
  - Change silo.postinst to automatically update the boot block without
    invoking siloconfig and keep asking questions on upgrades.
  - Convert silo.conf to use /dev/disk/by-uuid.
  - Ubuntu maintainer foobar.
  - Fix debian/rules call to dh_installdocs.
  - Drop the requirement of gcc-4.1 and start using default gcc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* prom - prom handling routines via /dev/openprom
 
2
   
 
3
   Copyright (C) 1996 Jakub Jelinek
 
4
   
 
5
   This program is free software; you can redistribute it and/or modify
 
6
   it under the terms of the GNU General Public License as published by
 
7
   the Free Software Foundation; either version 2 of the License, or
 
8
   (at your option) any later version.
 
9
   
 
10
   This program is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
   GNU General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU General Public License
 
16
   along with this program; if not, write to the Free Software
 
17
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 
18
   USA.  */
 
19
 
 
20
#include <stdio.h>
 
21
#include <string.h>
 
22
#include <stdlib.h>
 
23
#include <sys/ioctl.h>
 
24
#ifdef __linux__
 
25
#  include <asm/openpromio.h>
 
26
#elif defined (__sun__)
 
27
#  include <sys/types.h>
 
28
#  include <sys/stat.h>
 
29
#  include <sys/openpromio.h>
 
30
#  include <limits.h>
 
31
#else
 
32
#  error "Unknown system"
 
33
#endif
 
34
#include <fcntl.h>
 
35
 
 
36
static struct openpromio *oi;
 
37
static char buffer[2048 + sizeof (oi->oprom_size)];
 
38
static int prom_root_node = 0;
 
39
static int fd;
 
40
static int current_node;
 
41
static int previous_node;
 
42
static int action;
 
43
static int parent_node;
 
44
 
 
45
#define SETSIZE oi->oprom_size = 2048;
 
46
 
 
47
int prom_init (void)
 
48
{
 
49
    if ((fd = open ("/dev/openprom", O_RDONLY)) < 0 &&
 
50
        (fd = open ("/dev/misc/openprom", O_RDONLY)) < 0)
 
51
        return -1;
 
52
    oi = (struct openpromio *)buffer;
 
53
    SETSIZE
 
54
    *(int *)(oi->oprom_array) = 0;
 
55
    if (ioctl (fd, OPROMNEXT, (void *)oi) >= 0) {
 
56
        prom_root_node = *(int *)(oi->oprom_array);
 
57
        current_node = prom_root_node;
 
58
        previous_node = 0;
 
59
        parent_node = 0;
 
60
        action = 0;
 
61
        return 0;
 
62
    }
 
63
    return -1;
 
64
}
 
65
 
 
66
int prom_set_root_node (void)
 
67
{
 
68
    SETSIZE
 
69
    *(int *)(oi->oprom_array) = 0;
 
70
    if (ioctl (fd, OPROMNEXT, (void *)oi) >= 0) {
 
71
        current_node = prom_root_node;
 
72
        previous_node = 0;
 
73
        parent_node = 0;
 
74
        action = 0;
 
75
        return 0;
 
76
    }
 
77
    return -1;
 
78
}
 
79
 
 
80
int prom_search_siblings (char *name)
 
81
{
 
82
    int node = current_node;
 
83
    
 
84
    for (;;) {
 
85
        SETSIZE
 
86
        strcpy (oi->oprom_array, "name");
 
87
        if (ioctl (fd, OPROMGETPROP, (void *)oi) < 0)
 
88
            return -1;
 
89
        if (!oi->oprom_size) return -1;
 
90
        if (!strcmp (oi->oprom_array, name))
 
91
            return node;
 
92
        SETSIZE
 
93
        *(int *)(oi->oprom_array) = node;
 
94
        if (ioctl (fd, OPROMNEXT, (void *)oi) < 0 || !(*(int *)(oi->oprom_array)))
 
95
            return -1;
 
96
        if (!(*(int *)(oi->oprom_array)) || !oi->oprom_size) return -1;
 
97
        node = *(int *)(oi->oprom_array);
 
98
        previous_node = current_node;
 
99
        current_node = node;
 
100
        action = 0;
 
101
    }
 
102
}
 
103
 
 
104
int prom_next_sibling (void)
 
105
{
 
106
    SETSIZE
 
107
    *(int *)(oi->oprom_array) = current_node;
 
108
    if (ioctl (fd, OPROMNEXT, (void *)oi) < 0 || !(*(int *)(oi->oprom_array)))
 
109
        return -1;
 
110
    if (!(*(int *)(oi->oprom_array)) || !oi->oprom_size) return -1;
 
111
    previous_node = current_node;
 
112
    current_node = *(int *)(oi->oprom_array);
 
113
    action = 0;
 
114
    return 0;
 
115
}
 
116
 
 
117
int prom_getchild (void)
 
118
{
 
119
    SETSIZE
 
120
    *(int *)(oi->oprom_array) = current_node;
 
121
    if (ioctl (fd, OPROMCHILD, (void *)oi) >= 0) {
 
122
        if (!(*(int *)(oi->oprom_array)) || !oi->oprom_size) return -1;
 
123
        parent_node = current_node;
 
124
        current_node = *(int *)(oi->oprom_array);
 
125
        action = 1;
 
126
        previous_node = parent_node;
 
127
        return 0;
 
128
    }
 
129
    return -1;
 
130
}
 
131
 
 
132
char *prom_getstring (char *name)
 
133
{
 
134
    SETSIZE
 
135
    strcpy (oi->oprom_array, name);
 
136
    if (ioctl (fd, OPROMGETPROP, (void *)oi) >= 0) {
 
137
        if (!oi->oprom_size) return 0;
 
138
        return oi->oprom_array;
 
139
    }
 
140
    return 0;
 
141
}
 
142
 
 
143
char *prom_getopt (char *name)
 
144
{
 
145
    SETSIZE
 
146
    strcpy (oi->oprom_array, name);
 
147
    if (ioctl (fd, OPROMGETOPT, (void *)oi) >= 0) {
 
148
        if (!oi->oprom_size) return 0;
 
149
        return oi->oprom_array;
 
150
    }
 
151
    return 0;
 
152
}
 
153
 
 
154
#ifdef __sun__
 
155
int prom_getversion()
 
156
{
 
157
    int i;
 
158
    char arch[10], *p;
 
159
 
 
160
    SETSIZE
 
161
    if (ioctl (fd, OPROMGETVERSION, (void *)oi) >= 0) {
 
162
        p = strchr (oi->oprom_array, '.');
 
163
        if (p && p > oi->oprom_array && p[-1] >= '0' && p[-1] <= '9' && 
 
164
            (p == oi->oprom_array + 1 || p[-2] < '0' || p[-2] > '9')) {
 
165
            switch (p[-1]) {
 
166
                case '0':
 
167
                case '1': return 0;
 
168
                case '2':
 
169
                case '3': return 2;
 
170
            }
 
171
        }
 
172
        printf ("Please report version string '%s' to jj@sunsite.mff.cuni.cz\n", oi->oprom_array);
 
173
    }
 
174
    /* Let's try brute force :)) */
 
175
    
 
176
    if (prom_set_root_node () < 0) return -1;
 
177
    *arch = 0;
 
178
    if ((p = prom_getstring ("compatability")) != 0)
 
179
        strcpy (arch, p);
 
180
    else if ((p = prom_getstring ("compatible")) != 0)
 
181
        strcpy (arch, p);
 
182
    if (*arch && (!strcmp (arch, "sun4m") || !strcmp (arch, "sun4d") || !strcmp (arch, "sun4e")))
 
183
        return 2;
 
184
    else if (!*arch) {
 
185
        p = prom_getopt ("boot-from");
 
186
        if (p && (!strncmp (p, "sd(", 3) || 
 
187
                  !strncmp (p, "le(", 3) || 
 
188
                  !strncmp (p, "ie(", 3) ||
 
189
                  !strncmp (p, "fd(", 3) ||
 
190
                  !strncmp (p, "xd(", 3)))
 
191
            return 0;
 
192
    } else {
 
193
        if (prom_getchild () < 0) return -1;
 
194
        if (prom_search_siblings ("aliases") != -1) {
 
195
            p = prom_getstring ("disk");
 
196
            if (!p) p = prom_getstring ("net");
 
197
            if (!p) p = prom_getstring ("disk0");
 
198
            if (p && *p == '/') {
 
199
                if (!strncmp (p + 1, "iommu@", 6) || !strncmp (p + 1, "iommu/", 6) ||
 
200
                    !strncmp (p + 1, "sbus@", 5) || !strncmp (p + 1, "sbus/", 5) ||
 
201
                    !strncmp (p + 1, "espdma@", 7) || !strncmp (p + 1, "espdma/", 7) ||
 
202
                    !strncmp (p + 1, "esp@", 4) || !strncmp (p + 1, "esp/", 4) ||
 
203
                    !strncmp (p + 1, "io-unit@", 8) || !strncmp (p + 1, "io-unit/", 8))
 
204
                    return 2;
 
205
            }
 
206
        }
 
207
    }
 
208
    return -1;
 
209
}
 
210
#endif