summaryrefslogtreecommitdiffstats
path: root/loader2/hardware.c
blob: 6597b5d56b44bb2c0aa30c40c2ff5a374e6ddf8b (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
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
/*
 * hardware.c - various hardware probing functionality
 *
 * Erik Troan <ewt@redhat.com>
 * Matt Wilson <msw@redhat.com>
 * Michael Fulbright <msf@redhat.com>
 * Jeremy Katz <katzj@redhat.com>
 *
 * Copyright 1997 - 2003 Red Hat, Inc.
 *
 * This software may be freely redistributed under the terms of the GNU
 * General Public License.
 *
 * 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 <errno.h>
#include <fcntl.h>
#include <kudzu/kudzu.h>
#include <popt.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

#include "loader.h"
#include "hardware.h"
#include "pcmcia.h"
#include "log.h"

/* FIXME: for turning off dma */
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include "../isys/isys.h"


/* JKFIXME: this is the same hack as in loader.c for second stage modules */
extern struct moduleBallLocation * secondStageModuleLocation;

/* returns whether or not we can probe devices automatically or have to 
 * ask for them manually. */
int canProbeDevices(void) {
#if defined(__s390__) || defined(__s390x__)
    return 1;
#endif

    if ((access("/proc/bus/pci/devices", R_OK) &&
         access("/proc/openprom", R_OK) &&
         access("/proc/iSeries", R_OK)))
        return 0;

    return 1;    
}

static int detectHardware(moduleInfoSet modInfo, 
                          char *** modules, int flags) {
    struct device ** devices, ** device;
    char ** modList;
    int numMods;
    char *driver;
    
    logMessage("probing buses");
    
    devices = probeDevices(CLASS_UNSPEC,
                           BUS_PCI | BUS_SBUS | 
                           ((has_pcmcia() >= 0) ? BUS_PCMCIA : 0),
                           PROBE_ALL);

    logMessage("finished bus probing");
    
    if (devices == NULL) {
        *modules = NULL;
        return LOADER_OK;
    }
    
    numMods = 0;
    for (device = devices; *device; device++) numMods++;
    
    if (!numMods) {
        *modules = NULL;
        return LOADER_OK;
    }
    
    modList = malloc(sizeof(*modList) * (numMods + 1));
    numMods = 0;
    
    for (device = devices; *device; device++) {
        driver = (*device)->driver;
        /* this is kind of icky and verbose.  there are better and more 
         * general ways to do it but this is simple and obvious */
        if (FL_NOPCMCIA(flags) && ((*device)->type == CLASS_SOCKET)) {
            logMessage("ignoring pcmcia device %s (%s)", (*device)->desc,
                       (*device)->driver);
        } else if (FL_NOIEEE1394(flags) && ((*device)->type == CLASS_FIREWIRE)) {
            logMessage("ignoring firewire device %s (%s)", (*device)->desc,
                       (*device)->driver);
        } else if (FL_NOUSB(flags) && ((*device)->type == CLASS_USB)) {
            logMessage("ignoring usb device %s (%s)", (*device)->desc,
                       (*device)->driver);
        } else if (strcmp (driver, "ignore") && strcmp (driver, "unknown")
            && strcmp (driver, "disabled")) {
            modList[numMods++] = strdup(driver);
        }
        
        freeDevice (*device);
    }
    
    modList[numMods] = NULL;
    *modules = modList;
    
    free(devices);
    
    return LOADER_OK;
}

int agpgartInitialize(moduleList modLoaded, moduleDeps modDeps,
                      moduleInfoSet modInfo, int flags) {
    struct device ** devices, *p;
    int i;

    if (FL_TESTING(flags)) return 0;

    logMessage("looking for video cards requiring agpgart module");
    
    devices = probeDevices(CLASS_VIDEO, BUS_UNSPEC, 0);
    
    if (!devices) {
        logMessage("no video cards found");
        return 0;
    }

    /* loop thru cards, see if we need agpgart */
    for (i=0; devices[i]; i++) {
        p = devices[i];
        logMessage("found video card controller %s", p->driver);
        
        /* HACK - need to have list of cards which match!! */
        /* JKFIXME: verify this is really still needed */
        if (!strcmp(p->driver, "Card:Intel 810") ||
            !strcmp(p->driver, "Card:Intel 815")) {
            logMessage("found %s card requiring agpgart, loading module",
                       p->driver+5);
            
            if (mlLoadModuleSetLocation("agpgart", modLoaded, modDeps, 
					modInfo, flags, 
					secondStageModuleLocation)) {
                logMessage("failed to insert agpgart module");
                return 1;
            } else {
                /* only load it once! */
                return 0;
            }
        }
    }
    
    return 0;
}

int scsiTapeInitialize(moduleList modLoaded, moduleDeps modDeps,
                      moduleInfoSet modInfo, int flags) {
    struct device ** devices;

    if (FL_TESTING(flags)) return 0;

    logMessage("looking for scsi tape devices");
    
    devices = probeDevices(CLASS_TAPE, BUS_SCSI, 0);
    
    if (!devices) {
        logMessage("no scsi tape devices found");
        return 0;
    }

    logMessage("scsi tape device(s) found, loading st.ko");

    if (mlLoadModuleSetLocation("st", modLoaded, modDeps, 
				modInfo, flags, 
				secondStageModuleLocation)) {
	logMessage("failed to insert st module");
	return 1;
    }
    
    return 0;
}


/* This loads the necessary parallel port drivers for printers so that
   kudzu can autodetect and setup printers in post install*/
void initializeParallelPort(moduleList modLoaded, moduleDeps modDeps,
                            moduleInfoSet modInfo, int flags) {
    /* JKFIXME: this could be useful on other arches too... */
#if !defined (__i386__)
    return;
#endif
    if (FL_NOPARPORT(flags)) return;
    
    logMessage("loading parallel port drivers...");
    if (mlLoadModuleSetLocation("parport_pc", modLoaded, modDeps, 
				modInfo, flags,
				secondStageModuleLocation)) {
        logMessage("failed to load parport_pc module");
        return;
    }
}

int probeiSeries(moduleInfoSet modInfo, moduleList modLoaded, 
		 moduleDeps modDeps, int flags) {
    /* this is a hack since we can't really probe on iSeries */
#ifdef __powerpc__
    if (!access("/proc/iSeries", X_OK)) {
	mlLoadModuleSet("iseries_veth:veth:viodasd:viocd", modLoaded, modDeps, modInfo, flags);
    }
#endif
    return 0;
}

/* this allows us to do an early load of modules specified on the
 * command line to allow automating the load order of modules so that
 * eg, certain scsi controllers are definitely first.
 * FIXME: this syntax is likely to change in a future release
 *        but is done as a quick hack for the present.
 */
int earlyModuleLoad(moduleInfoSet modInfo, moduleList modLoaded, 
                    moduleDeps modDeps, int justProbe, int flags) {
    int fd, len, i;
    char buf[1024], *cmdLine;
    int argc;
    char ** argv;

    /* FIXME: reparsing /proc/cmdline to avoid major loader changes.  
     * should probably be done in loader.c:parseCmdline() like everything 
     * else
     */
    if ((fd = open("/proc/cmdline", O_RDONLY)) < 0) return 1;
    len = read(fd, buf, sizeof(buf) - 1);
    close(fd);
    if (len <= 0) return 1;
        
    buf[len] = '\0';
    cmdLine = buf;
    
    if (poptParseArgvString(cmdLine, &argc, (const char ***) &argv))
        return 1;
    
    for (i=0; i < argc; i++) {
        if (!strncasecmp(argv[i], "driverload=", 11)) {
            logMessage("loading %s early", argv[i] + 11);
            mlLoadModuleSet(argv[i] + 11, modLoaded, modDeps, modInfo, flags);
        }
    }
    return 0;
}

int busProbe(moduleInfoSet modInfo, moduleList modLoaded, moduleDeps modDeps,
             int justProbe, int flags) {
    int i;
    char ** modList;
    char modules[1024];
    
    /* we always want to try to find out about pcmcia controllers even
     * if using noprobe */
    initializePcmciaController(modLoaded, modDeps, modInfo, flags);

    if (FL_NOPROBE(flags)) return 0;

    /* we can't really *probe* on iSeries, but we can pretend */
    probeiSeries(modInfo, modLoaded, modDeps, flags);
    
    if (canProbeDevices()) {
        /* autodetect whatever we can */
        if (detectHardware(modInfo, &modList, flags)) {
            logMessage("failed to scan pci bus!");
            return 0;
        } else if (modList && justProbe) {
            for (i = 0; modList[i]; i++)
                printf("%s\n", modList[i]);
        } else if (modList) {
            *modules = '\0';
            
            for (i = 0; modList[i]; i++) {
                if (i) strcat(modules, ":");
                strcat(modules, modList[i]);
            }
            
            mlLoadModuleSet(modules, modLoaded, modDeps, modInfo, flags);

            startPcmciaDevices(modLoaded, flags);
        } else 
            logMessage("found nothing");
    }
    
    return 0;
}


void scsiSetup(moduleList modLoaded, moduleDeps modDeps,
               moduleInfoSet modInfo, int flags) {
    mlLoadModuleSet("scsi_mod:sd_mod:sr_mod", modLoaded, modDeps, modInfo, flags);
}

void ideSetup(moduleList modLoaded, moduleDeps modDeps,
              moduleInfoSet modInfo, int flags) {
#if 0
    struct device ** devices;
    int fd, i;
#endif

    mlLoadModuleSet("cdrom:ide-cd", modLoaded, modDeps, modInfo, flags);

    /* nuke this for now */
#if 0
    /* FIXME: having dma on for CD devices seems to break media check
     * as well as causing other problems for people.  To avoid having
     * to tell everyone to use ide=nodma all the time, let's do it 
     * ourselves.
     */
    if (FL_ENABLECDDMA(flags))
        return;
    devices = probeDevices(CLASS_CDROM, BUS_IDE, 0);
    if (!devices)
        return;

    for (i = 0; devices[i]; i++) {
        if ((devices[i]->detached != 0) || (devices[i]->device == NULL)) 
            continue;
        devMakeInode(devices[i]->device, "/tmp/cdrom");
        fd = open("/tmp/cdrom", O_RDONLY|O_NONBLOCK);
        if (fd == -1) {
            logMessage("failed to open /tmp/cdrom: %s", strerror(errno));
            unlink("/tmp/cdrom");
            continue;
        }
        if (ioctl(fd, HDIO_SET_DMA, 0) == -1)
            logMessage("failed to disable dma for %s: %s", devices[i]->device,
                       strerror(errno));
        else
            logMessage("disabled DMA for CD devices %s", devices[i]->device);
        close(fd);
        unlink("/tmp/cdrom");
    }
#endif
    
}


/* check if the system has been booted with dasd parameters */
/* These parameters define the order in which the DASDs */
/* are visible to Linux. Otherwise load dasd modules probeonly, */
/* then parse proc to find active DASDs */
/* Reload dasd_mod with correct range of DASD ports */
void dasdSetup(moduleList modLoaded, moduleDeps modDeps,
               moduleInfoSet modInfo, int flags) {
#if !defined(__s390__) && !defined(__s390x__)
    return;
#else
    char **dasd_parms;
    char *line;
    char *parms = NULL, *parms_end;
    FILE *fd;

    dasd_parms = malloc(sizeof(*dasd_parms) * 2);
    dasd_parms[0] = NULL;
    dasd_parms[1] = NULL;

    fd = fopen ("/tmp/dasd_ports", "r");
    if(fd) {
        line = (char *)malloc(sizeof(char) * 200);
        while (fgets (line, 199, fd) != NULL) {
            if((parms = strstr(line, "dasd=")) ||
               (parms = strstr(line, "DASD="))) {
                strncpy(parms, "dasd", 4);
                parms_end = parms;
                while(*parms_end && !(isspace(*parms_end))) parms_end++;
                *parms_end = '\0';
                break;
            }
        }
        fclose(fd);
        free(line);
    }
    if(!parms || (strlen(parms) == 5)) {
        parms = NULL;
    } else {
        dasd_parms[0] = strdup(parms);
        mlLoadModule("dasd_mod", modLoaded, modDeps, modInfo,
                     dasd_parms, flags);

        mlLoadModuleSet("dasd_diag_mod:dasd_fba_mod:dasd_eckd_mod", 
                        modLoaded, modDeps, modInfo, flags);
        return;
    }
    if(!parms) {
        dasd_parms[0] = "dasd=autodetect";
        mlLoadModule("dasd_mod", modLoaded, modDeps, modInfo, dasd_parms, flags);
        mlLoadModuleSet("dasd_diag_mod:dasd_fba_mod:dasd_eckd_mod",
                        modLoaded, modDeps, modInfo, flags);
        free(dasd_parms);
    }
#endif
}