[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Autodetection of CD-ROM devices



The attached program is a prototype for a autodetection function to be
put in the choose_medium.c file of dbootstrap.  It seems to handle the
"new" 2.2 IDE subsystem and SCSI devices since at least 2.0; I've
tested it on m68k, alpha and ia32 platforms.  Since the IDE system
treats DVD-ROMs as CD-ROMs, they are properly detected (though it's
possible /proc/ide/hd?/media may report "dvd-rom" or something else in
2.3+; I haven't checked).

Note the use of a bitmask for the IDE detection; SCSI devices are
simply counted since they are numbered 0..n at boot time.  Non-IDE and
non-SCSI CD-ROMs aren't handled at all.

My suggested use would be for the routine to "mark" the list presented
by choose_cdrom(), although (presumably) if only one matching device
is detected, it could bypass the menu altogether.

Anyway, it's packaged as a standalone program so people can test it
and let me know if it fails to detect a CD-ROM it should, or detects
something wrongly.


Chris
-- 
=============================================================================
|        Chris Lawrence       |  Your source for almost nothing of value:   |
|   <quango@watervalley.net>  |          http://www.lordsutch.com/          |
|                             |                                             |
|   Grad Student, Pol. Sci.   |   Visit the Lurker's Guide to Babylon 5:    |
|  University of Mississippi  |   <*> http://www.midwinter.com/lurk/ <*>    |
=============================================================================
#include <stdio.h>
#include <sys/stat.h>
#include <glob.h>

struct cdroms_detected {
    int scsi_count;
    int ide_mask;
};

static struct cdroms_detected autodetect_cdrom(void)
{
    FILE *fp;
    struct stat st;
    glob_t gl;
    static struct cdroms_detected cdr = {0,0};
    size_t sz=100;
    char *buf;
    
    buf = (char *)malloc(sz);
    
    if(!stat("/proc/ide", &st) && S_ISDIR(st.st_mode)) {
	if(!glob("/proc/ide/hd?/media", 0, NULL, &gl)) {
	    int i;
	    
	    for( i = 0; i < gl.gl_pathc; i++ ) {
		if(fp = fopen(gl.gl_pathv[i], "r")) {
		    if(getline(&buf, &sz, fp) >= 0 &&
		       !strncmp(buf, "cdrom", 5)) {
			printf("CD-ROM: /dev/hd%c\n", gl.gl_pathv[i][12]);
			cdr.ide_mask |= 1<< (gl.gl_pathv[i][12] - 'a');
		    }
		    /* printf("%s: %s", gl.gl_pathv[i], buf); */
		    fclose(fp);
		}
	    }
	    globfree(&gl);
	}
    }

    if(!stat("/proc/scsi/scsi", &st) && S_ISREG(st.st_mode)) {
	if(fp = fopen("/proc/scsi/scsi", "r")) {
	    while(!feof(fp) && getline(&buf, &sz, fp) >= 0) {
		/* fputs(buf, stdout); */
		if(!strncmp(buf, "  Type:   CD-ROM", 16)) {
		    printf("CD-ROM: /dev/scd%d\n", cdr.scsi_count);
		    cdr.scsi_count++;
		}
	    }
	}
    }
    
    return cdr;
}

int main(void)
{
    autodetect_cdrom();
}

Reply to: