/* * Guillaume Cottenceau (gc@mandrakesoft.com) * * Copyright 2000 MandrakeSoft * * This software may be freely redistributed under the terms of the GNU * 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. * */ /* * Portions from Erik Troan (ewt@redhat.com) * * Copyright 1996 Red Hat Software * */ /* * This contains stuff related to probing: * (1) any (actually only SCSI, NET, CPQ, USB Controllers) devices (autoprobe for PCI and USB) * (2) IDE media * (3) SCSI media * (4) ETH devices */ #include #include #include #include #include #include #include #include #include #include #include #include #include "stage1.h" #include "log.h" #include "frontend.h" #include "modules.h" #include "probing.h" struct media_info { char * name; char * model; enum media_type type; }; #ifndef DISABLE_NETWORK struct net_description_elem { char * intf_name; char * intf_description; }; static char * net_dev_black_list[] = {"lo", "wlan", /* add what you need here! */ NULL}; static struct net_description_elem net_descriptions[50]; static int net_descr_number = 0; static char * intf_descr_for_discover = NULL; static char * net_intf_too_early_name[50]; /* for modules providing more than one net intf */ static int net_intf_too_early_number = 0; static int net_intf_too_early_ptr = 0; void net_discovered_interface(char * intf_name) { if (!intf_descr_for_discover) { net_intf_too_early_name[net_intf_too_early_number++] = strdup(intf_name); return; } if (!intf_name) { if (net_intf_too_early_ptr >= net_intf_too_early_number) { log_message("NET: was expecting another network interface (broken net module?)"); return; } net_descriptions[net_descr_number].intf_name = net_intf_too_early_name[net_intf_too_early_ptr++]; } else net_descriptions[net_descr_number].intf_name = strdup(intf_name); net_descriptions[net_descr_number].intf_description = strdup(intf_descr_for_discover); intf_descr_for_discover = NULL; net_descr_number++; } char * get_net_intf_description(char * intf_name) { char dev[SYSFS_PATH_MAX]; char drv[SYSFS_PATH_MAX]; ssize_t i; snprintf(dev, SYSFS_PATH_MAX, "/sys/class/net/%s/device/driver", intf_name); if ((i = readlink(dev, drv, SYSFS_PATH_MAX)) > 0) { drv[i] = '\0'; return strdup(strrchr(drv, '/') + 1); } else { return strdup("unknown"); } } #endif #ifndef DISABLE_MEDIAS static struct media_info * medias = NULL; static int usb_not_there(void) { DIR *dir = opendir("/sys/module/usb_storage"); if (dir) closedir(dir); return !dir; } static void find_media(void) { FILE *f; DIR *dir = NULL; struct dirent *dirent = NULL; struct stat st; struct media_info tmp[50]; char path[SYSFS_PATH_MAX]; char buf[512]; char *s; int i = 0, count = 0; if (medias) free(medias); /* that does not free the strings, by the way */ log_message("checking for /sys/module/usb_storage directory"); if (usb_not_there()) { wait_message("Waiting for USB storage devices to show up..."); my_insmod("usb_storage", NULL); my_insmod("sd_mod", NULL); my_insmod("uas", NULL); while (usb_not_there() && (i < 10)) { sleep(1); i++; } remove_wait_message(); } if ((dir = opendir("/sys/block")) == NULL) { log_message("failed to open /sys/block directory"); return; } while ((dirent = readdir(dir)) != NULL) { if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..")) continue; memset(path, 0, SYSFS_PATH_MAX); strcpy(path, "/sys/block/"); strcat(path, dirent->d_name); s = path + strlen(path); /* probe for scsi type */ strcat(path, "/device/type"); if (lstat(path, &st) == 0 && S_ISREG(st.st_mode)) { tmp[count].name = strdup(dirent->d_name); tmp[count].type = UNKNOWN_MEDIA; if ((f = fopen(path, "r")) != NULL) { int type = SCSI_TYPE_DISK; if (fgets(buf, sizeof(buf), f) > 0) { if (!strncmp(buf, "MMC", 3) || !strncmp(buf, "SD", 2)) tmp[count].type = DISK; else if (sscanf(buf, "%d", &type) > 0) { if (type == SCSI_TYPE_DISK) tmp[count].type = DISK; else if (type == SCSI_TYPE_ROM) tmp[count].type = CDROM; else if (type == SCSI_TYPE_TAPE) tmp[count].type = TAPE; } } fclose(f); } *s = 0; strcat(path, "/device/model"); if ((f = fopen(path, "r")) != NULL) { if (fgets(buf, sizeof(buf), f)) { if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; tmp[count].model = strdup(buf); } fclose(f); } else { tmp[count].model = strdup("(unknown)"); } log_message("SCSI/%d: %s is a %s", tmp[count].type, tmp[count].name, tmp[count].model); count++; continue; } /* assume ide */ *s = 0; strcat(s, "/device/media"); if (lstat(path, &st) == 0 && S_ISREG(st.st_mode)) { tmp[count].name = strdup(dirent->d_name); tmp[count].type = UNKNOWN_MEDIA; if ((f = fopen(path, "r")) != NULL) { if (fgets(buf, sizeof(buf), f)) { if (!strncmp("disk", buf, 4)) tmp[count].type = DISK; else if (!strncmp("cdrom", buf, 5)) tmp[count].type = CDROM; else if (!strncmp("tape", buf, 4)) tmp[count].type = TAPE; else if (!strncmp("floppy", buf, 6)) tmp[count].type = FLOPPY; } fclose(f); } /* grab model */ strcpy(path, "/proc/ide/"); strcat(path, dirent->d_name); strcat(path, "/model"); if ((f = fopen(path, "r")) != NULL) { if (fgets(buf, sizeof(buf), f)) { if(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; tmp[count].model = strdup(buf); } fclose(f); } else { tmp[count].model = strdup("(none)"); } log_message("IDE/%d: %s is a %s", tmp[count].type, tmp[count].name, tmp[count].model); count++; } } closedir(dir); tmp[count].name = NULL; count++; medias = memdup(tmp, sizeof(struct media_info) * count); } /* Finds by media */ void get_medias(enum media_type media, char *** names, char *** models) { struct media_info * m; char * tmp_names[250]; char * tmp_models[250]; int count; find_media(); m = medias; count = 0; while (m && m->name) { /* detect hybrid iso images (those dumped to a USB stick) */ if ((m->type == media) || ((media == CDROM) && (m->type == DISK))) { tmp_names[count] = strdup(m->name); tmp_models[count++] = strdup(m->model); } m++; } tmp_names[count] = NULL; tmp_models[count++] = NULL; *names = memdup(tmp_names, sizeof(char *) * count); *models = memdup(tmp_models, sizeof(char *) * count); } #endif /* DISABLE_MEDIAS */ #ifndef DISABLE_NETWORK static int net_device_has_carrier(const char *device) { int fd = -1; char *p = NULL; int ret = 0; char state = '\0'; if (asprintf(&p, "/sys/class/net/%s/carrier", device) == -1) { log_message("net_device_has_carrier: OOM"); return 0; } fd = open(p, O_RDONLY); if (fd < 0) { log_perror("net_device_has_carrier: failed to open sysfs knob"); goto out; } if (read(fd, &state, 1) != 1) { log_perror("net_device_has_carrier: failed to read sysfs knob"); goto out; } ret = !!(state == '1'); out: if (p) { free(p); p = NULL; } if (fd >= 0) { close(fd); fd = -1; } return ret; } static int net_device_available(char * device) { struct ifreq req; int s; s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { log_perror(device); return 0; } strcpy(req.ifr_name, device); if (ioctl(s, SIOCGIFFLAGS, &req)) { /* if we can't get the flags, the networking device isn't available */ close(s); return 0; } if (!(req.ifr_flags & IFF_UP)) { req.ifr_flags |= IFF_UP; if (ioctl(s, SIOCSIFFLAGS, &req)) { log_perror("ifup"); /* skip the interface we can't bring up */ } /* XXX: we want to report only interfaces with carrier as available. * However calling net_device_has_carrier right after ifup is useless: * the kernel needs some time to figure out if the carrier is present. * Therefore report that this interface is *not* available. * intf_select_and_up will wait a second and retry if necessary * (i.e. no interfaces with carrier have been found), and on the next * attempt `device` will be already up, and we'll check if `device` * has the carrier. * * Sounds a bit tricky? A simple solution is to put sleep(1) after * ifup, but there might be quite a number of interfaces (I've got * boards with 4 and 6 Ethernets), and sleeping for O(N) seconds * for no good reason might be annoying. * Oh, alternatively we could setup a full-fledged event loop and * wait for netlink notifications, but that's too much code and * *complexity*. */ close(s); return 0; } /* `device` was already up (perhaps we've brought it up during * previous calls), check if it has the carrier */ close(s); return net_device_has_carrier(device); } char ** get_net_devices(void) { DIR * sys_net; char * tmp[50] = {NULL}; struct dirent * ent; int drop, j, i = 0; sys_net = opendir("/sys/class/net"); if (sys_net == NULL) return memdup(tmp, sizeof(tmp[0])); while ((ent = readdir(sys_net)) != NULL) { /* check interface name in black list */ for (drop=j=0; net_dev_black_list[j] != NULL; j++) if (strncmp(ent->d_name, net_dev_black_list[j], strlen(net_dev_black_list[j])) == 0) { drop = -1; break; } if (!drop && net_device_available(ent->d_name)) { tmp[i++] = strdup(ent->d_name); } } tmp[i++] = NULL; closedir(sys_net); return memdup(tmp, sizeof(tmp[0]) * i); } /* reverse for get_net_devices() to avoid memory leaks */ void free_net_devices(char ** list) { char ** item; for (item = list; *item; item++) { free(*item); *item = NULL; } free(list); } #endif /* DISABLE_NETWORK */