2 * The new sysinstall program.
4 * This is probably the last program in the `sysinstall' line - the next
5 * generation being essentially a complete rewrite.
10 * Jordan Hubbard. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer,
17 * verbatim and that no modifications are made prior to this
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "sysinstall.h"
38 #include <sys/fcntl.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
42 #include <sys/errno.h>
45 #include <net/if_var.h>
46 #include <net/if_dl.h>
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49 #include <arpa/inet.h>
52 /* how much to bias minor number for a given /dev/<ct#><un#>s<s#> slice */
53 #define SLICE_DELTA (0x10000)
55 static Device *Devices[DEV_MAX];
58 static struct _devname {
62 int major, minor, delta, max;
65 { DEVICE_TYPE_CDROM, "cd%dc", "SCSI CDROM drive", 6, 2, 8, 4, 'b' },
66 { DEVICE_TYPE_CDROM, "mcd%da", "Mitsumi (old model) CDROM drive", 7, 0, 8, 4, 'b' },
67 { DEVICE_TYPE_CDROM, "scd%da", "Sony CDROM drive - CDU31/33A type", 16, 0, 8, 4, 'b' },
69 { DEVICE_TYPE_CDROM, "matcd%da", "Matsushita CDROM ('sound blaster' type)", 17, 0, 8, 4, 'b' },
71 { DEVICE_TYPE_CDROM, "acd%dc", "ATAPI/IDE CDROM", 31, 2, 8, 4, 'b' },
72 { DEVICE_TYPE_TAPE, "rsa%d", "SCSI tape drive", 14, 0, 16, 4, 'c' },
73 { DEVICE_TYPE_TAPE, "rwt%d", "Wangtek tape drive", 10, 0, 1, 4, 'c' },
74 { DEVICE_TYPE_DISK, "da%d", "SCSI disk device", 4, 65538, 8, 16, 'b' },
75 { DEVICE_TYPE_DISK, "rda%d", "SCSI disk device", 13, 65538, 8, 16, 'c' },
76 { DEVICE_TYPE_DISK, "ad%d", "ATA/IDE disk device", 30, 65538, 8, 16, 'b' },
77 { DEVICE_TYPE_DISK, "rad%d", "ATA/IDE disk device", 116, 65538, 8, 16, 'c' },
78 { DEVICE_TYPE_DISK, "fla%d", "M-Systems DiskOnChip Flash device", 28, 65538, 8, 16, 'b' },
79 { DEVICE_TYPE_DISK, "rfla%d", "M-Systems DiskOnChip Flash devicee", 102, 65538, 8, 16, 'c' },
80 { DEVICE_TYPE_DISK, "afd%d", "ATAPI/IDE floppy device", 32, 65538, 8, 4, 'b' },
81 { DEVICE_TYPE_DISK, "rafd%d", "ATAPI/IDE floppy device", 118, 65538, 8, 4, 'c' },
82 { DEVICE_TYPE_DISK, "mlxd%d", "Mylex RAID disk", 27, 65538, 8, 4, 'b' },
83 { DEVICE_TYPE_DISK, "rmlxd%d", "Mylex RAID disk", 131, 65538, 8, 4, 'c' },
84 { DEVICE_TYPE_DISK, "amrd%d", "AMI MegaRAID drive", 35, 65538, 8, 4, 'b' },
85 { DEVICE_TYPE_DISK, "ramrd%d", "AMI MegaRAID drive", 133, 65538, 8, 4, 'c' },
86 { DEVICE_TYPE_FLOPPY, "fd%d", "floppy drive unit A", 2, 0, 64, 4, 'b' },
87 { DEVICE_TYPE_FLOPPY, "worm%d", "SCSI optical disk / CDR", 23, 0, 1, 4, 'b' },
88 { DEVICE_TYPE_NETWORK, "aue", "ADMtek USB ethernet adapter" },
89 { DEVICE_TYPE_NETWORK, "cue", "CATC USB ethernet adapter" },
90 { DEVICE_TYPE_NETWORK, "fpa", "DEC DEFPA PCI FDDI card" },
91 { DEVICE_TYPE_NETWORK, "sr", "SDL T1/E1 sync serial PCI card" },
92 { DEVICE_TYPE_NETWORK, "cc3i", "SDL HSSI sync serial PCI card" },
93 { DEVICE_TYPE_NETWORK, "en", "Efficient Networks ATM PCI card" },
94 { DEVICE_TYPE_NETWORK, "dc", "DEC/Intel 21143 (and clones) PCI fast ethernet card" },
95 { DEVICE_TYPE_NETWORK, "de", "DEC DE435 PCI NIC or other DC21040-AA based card" },
96 { DEVICE_TYPE_NETWORK, "fxp", "Intel EtherExpress Pro/100B PCI Fast Ethernet card" },
97 { DEVICE_TYPE_NETWORK, "ed", "Novell NE1000/2000; 3C503; NE2000-compatible PCMCIA" },
98 { DEVICE_TYPE_NETWORK, "ep", "3Com 3C509 ethernet card/3C589 PCMCIA" },
99 { DEVICE_TYPE_NETWORK, "el", "3Com 3C501 ethernet card" },
100 { DEVICE_TYPE_NETWORK, "ex", "Intel EtherExpress Pro/10 ethernet card" },
101 { DEVICE_TYPE_NETWORK, "fe", "Fujitsu MB86960A/MB86965A ethernet card" },
102 { DEVICE_TYPE_NETWORK, "ie", "AT&T StarLAN 10 and EN100; 3Com 3C507; NI5210" },
103 { DEVICE_TYPE_NETWORK, "ix", "Intel Etherexpress ethernet card" },
104 { DEVICE_TYPE_NETWORK, "kue", "Kawasaki LSI USB ethernet adapter" },
105 { DEVICE_TYPE_NETWORK, "le", "DEC EtherWorks 2 or 3 ethernet card" },
106 { DEVICE_TYPE_NETWORK, "lnc", "Lance/PCnet (Isolan/Novell NE2100/NE32-VL) ethernet" },
107 { DEVICE_TYPE_NETWORK, "rl", "RealTek 8129/8139 PCI ethernet card" },
108 { DEVICE_TYPE_NETWORK, "sf", "Adaptec AIC-6915 PCI ethernet card" },
109 { DEVICE_TYPE_NETWORK, "sis", "SiS 900/SiS 7016 PCI ethernet card" },
110 { DEVICE_TYPE_NETWORK, "sn", "SMC/Megahertz ethernet card" },
111 { DEVICE_TYPE_NETWORK, "ste", "Sundance ST201 PCI ethernet card" },
112 { DEVICE_TYPE_NETWORK, "sk", "SysKonnect PCI gigabit ethernet card" },
113 { DEVICE_TYPE_NETWORK, "tx", "SMC 9432TX ethernet card" },
114 { DEVICE_TYPE_NETWORK, "ti", "Alteon Networks PCI gigabit ethernet card" },
115 { DEVICE_TYPE_NETWORK, "tl", "Texas Instruments ThunderLAN PCI ethernet card" },
116 { DEVICE_TYPE_NETWORK, "vr", "VIA VT3043/VT86C100A Rhine PCI ethernet card" },
117 { DEVICE_TYPE_NETWORK, "vx", "3COM 3c590 / 3c595 ethernet card" },
118 { DEVICE_TYPE_NETWORK, "wb", "Winbond W89C840F PCI ethernet card" },
119 { DEVICE_TYPE_NETWORK, "xl", "3COM 3c90x / 3c90xB PCI ethernet card" },
120 { DEVICE_TYPE_NETWORK, "cuaa%d", "%s on device %s (COM%d)", 28, 128, 1, 16, 'c' },
121 { DEVICE_TYPE_NETWORK, "lp", "Parallel Port IP (PLIP) peer connection" },
122 { DEVICE_TYPE_NETWORK, "lo", "Loop-back (local) network interface" },
127 new_device(char *name)
131 dev = safe_malloc(sizeof(Device));
132 bzero(dev, sizeof(Device));
134 SAFE_STRCPY(dev->name, name);
138 /* Stubs for unimplemented strategy routines */
140 dummyInit(Device *dev)
146 dummyGet(Device *dev, char *dist, Boolean probe)
152 dummyShutdown(Device *dev)
158 deviceTry(struct _devname dev, char *try, int i)
166 snprintf(unit, sizeof unit, dev.name, i);
167 snprintf(try, FILENAME_MAX, "/dev/%s", unit);
168 fd = open(try, O_RDONLY);
172 if (dev.dev_type == 'c')
176 d = makedev(dev.major, dev.minor + (i * dev.delta));
177 fail = mknod(try, m, d);
178 fd = open(try, O_RDONLY);
183 /* Don't try a "make-under" here since we're using a fixit floppy in this case */
184 snprintf(try, FILENAME_MAX, "/mnt/dev/%s", unit);
185 fd = open(try, O_RDONLY);
189 /* Register a new device in the devices array */
191 deviceRegister(char *name, char *desc, char *devname, DeviceType type, Boolean enabled,
192 Boolean (*init)(Device *), FILE * (*get)(Device *, char *, Boolean),
193 void (*shutdown)(Device *), void *private)
195 Device *newdev = NULL;
197 if (numDevs == DEV_MAX)
198 msgFatal("Too many devices found!");
200 newdev = new_device(name);
201 newdev->description = desc;
202 newdev->devname = devname;
204 newdev->enabled = enabled;
205 newdev->init = init ? init : dummyInit;
206 newdev->get = get ? get : dummyGet;
207 newdev->shutdown = shutdown ? shutdown : dummyShutdown;
208 newdev->private = private;
209 Devices[numDevs] = newdev;
210 Devices[++numDevs] = NULL;
215 /* Reset the registered device chain */
221 for (i = 0; i < numDevs; i++) {
222 Devices[i]->shutdown(Devices[i]);
224 /* XXX this potentially leaks Devices[i]->private if it's being
225 * used to point to something dynamic, but you're not supposed
226 * to call this routine at such times that some open instance
227 * has its private ptr pointing somewhere anyway. XXX
231 Devices[numDevs = 0] = NULL;
234 /* Get all device information for devices we have attached */
240 struct ifreq *ifptr, *end;
242 char buffer[INTERFACE_MAX * sizeof(struct ifreq)];
245 msgNotify("Probing devices, please wait (this can take a while)...");
246 /* First go for the network interfaces. Stolen shamelessly from ifconfig! */
247 ifc.ifc_len = sizeof(buffer);
248 ifc.ifc_buf = buffer;
250 s = socket(AF_INET, SOCK_DGRAM, 0);
252 goto skipif; /* Jump over network iface probing */
254 if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0)
255 goto skipif; /* Jump over network iface probing */
257 ifflags = ifc.ifc_req->ifr_flags;
258 end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
259 for (ifptr = ifc.ifc_req; ifptr < end; ifptr++) {
262 /* If it's not a link entry, forget it */
263 if (ifptr->ifr_ifru.ifru_addr.sa_family != AF_LINK)
266 /* Eliminate network devices that don't make sense */
267 if (!strncmp(ifptr->ifr_name, "lo", 2))
270 /* If we have a slip device, don't register it */
271 if (!strncmp(ifptr->ifr_name, "sl", 2)) {
274 /* And the same for ppp */
275 if (!strncmp(ifptr->ifr_name, "tun", 3) || !strncmp(ifptr->ifr_name, "ppp", 3)) {
278 /* Try and find its description */
279 for (i = 0, descr = NULL; device_names[i].name; i++) {
280 int len = strlen(device_names[i].name);
282 if (!ifptr->ifr_name || !ifptr->ifr_name[0])
284 else if (!strncmp(ifptr->ifr_name, device_names[i].name, len)) {
285 descr = device_names[i].description;
290 descr = "<unknown network interface type>";
292 deviceRegister(ifptr->ifr_name, descr, strdup(ifptr->ifr_name), DEVICE_TYPE_NETWORK, TRUE,
293 mediaInitNetwork, NULL, mediaShutdownNetwork, NULL);
294 msgDebug("Found a network device named %s\n", ifptr->ifr_name);
296 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
300 if (ifptr->ifr_addr.sa_len) /* I'm not sure why this is here - it's inherited */
301 ifptr = (struct ifreq *)((caddr_t)ifptr + ifptr->ifr_addr.sa_len - sizeof(struct sockaddr));
305 /* Next, try to find all the types of devices one might need
306 * during the second stage of the installation.
308 for (i = 0; device_names[i].name; i++) {
309 for (j = 0; j < device_names[i].max; j++) {
310 char try[FILENAME_MAX];
312 switch(device_names[i].type) {
313 case DEVICE_TYPE_CDROM:
314 fd = deviceTry(device_names[i], try, j);
315 if (fd >= 0 || errno == EBUSY) { /* EBUSY if already mounted */
318 if (fd >= 0) close(fd);
319 snprintf(n, sizeof n, device_names[i].name, j);
320 deviceRegister(strdup(n), device_names[i].description, strdup(try),
321 DEVICE_TYPE_CDROM, TRUE, mediaInitCDROM, mediaGetCDROM,
322 mediaShutdownCDROM, NULL);
323 msgDebug("Found a CDROM device for %s\n", try);
327 case DEVICE_TYPE_TAPE:
328 fd = deviceTry(device_names[i], try, j);
333 snprintf(n, sizeof n, device_names[i].name, j);
334 deviceRegister(strdup(n), device_names[i].description, strdup(try),
335 DEVICE_TYPE_TAPE, TRUE, mediaInitTape, mediaGetTape, mediaShutdownTape, NULL);
336 msgDebug("Found a TAPE device for %s\n", try);
340 case DEVICE_TYPE_DISK:
341 fd = deviceTry(device_names[i], try, j);
342 if (fd >= 0 && RunningAsInit) {
346 char unit[80], slice[80];
349 /* Make associated slice entries */
350 for (s = 1; s < 8; s++) {
351 snprintf(unit, sizeof unit, device_names[i].name, j);
352 snprintf(slice, sizeof slice, "/dev/%ss%d", unit, s);
353 d = makedev(device_names[i].major, device_names[i].minor +
354 (j * device_names[i].delta) + (s * SLICE_DELTA));
356 if (device_names[i].dev_type == 'c')
360 fail = mknod(slice, m, d);
361 fd = open(slice, O_RDONLY);
370 case DEVICE_TYPE_FLOPPY:
371 fd = deviceTry(device_names[i], try, j);
376 snprintf(n, sizeof n, device_names[i].name, j);
377 deviceRegister(strdup(n), device_names[i].description, strdup(try),
378 DEVICE_TYPE_FLOPPY, TRUE, mediaInitFloppy, mediaGetFloppy,
379 mediaShutdownFloppy, NULL);
380 msgDebug("Found a floppy device for %s\n", try);
384 case DEVICE_TYPE_NETWORK:
385 fd = deviceTry(device_names[i], try, j);
386 /* The only network devices that you can open this way are serial ones */
391 cp = device_names[i].description;
392 /* Serial devices get a slip and ppp device each, if supported */
393 newdesc = safe_malloc(strlen(cp) + 40);
394 sprintf(newdesc, cp, "SLIP interface", try, j + 1);
395 deviceRegister("sl0", newdesc, strdup(try), DEVICE_TYPE_NETWORK, TRUE, mediaInitNetwork,
396 NULL, mediaShutdownNetwork, NULL);
397 msgDebug("Add mapping for %s to sl0\n", try);
398 newdesc = safe_malloc(strlen(cp) + 50);
399 sprintf(newdesc, cp, "PPP interface", try, j + 1);
400 deviceRegister("ppp0", newdesc, strdup(try), DEVICE_TYPE_NETWORK, TRUE, mediaInitNetwork,
401 NULL, mediaShutdownNetwork, NULL);
402 msgDebug("Add mapping for %s to ppp0\n", try);
412 /* Finally, go get the disks and look for DOS partitions to register */
413 if ((names = Disk_Names()) != NULL) {
416 for (i = 0; names[i]; i++) {
420 d = Open_Disk(names[i]);
422 msgFatal("Unable to open disk %s", names[i]);
424 deviceRegister(names[i], names[i], d->name, DEVICE_TYPE_DISK, FALSE,
425 dummyInit, dummyGet, dummyShutdown, d);
426 msgDebug("Found a disk device named %s\n", names[i]);
428 /* Look for existing DOS partitions to register as "DOS media devices" */
429 for (c1 = d->chunks->part; c1; c1 = c1->next) {
430 if (c1->type == fat || c1->type == extended) {
435 snprintf(devname, sizeof devname, "/dev/%s", c1->name);
436 dev = deviceRegister(c1->name, c1->name, strdup(devname), DEVICE_TYPE_DOS, TRUE,
437 mediaInitDOS, mediaGetDOS, mediaShutdownDOS, NULL);
439 msgDebug("Found a DOS partition %s on drive %s\n", c1->name, d->name);
445 dialog_clear_norefresh();
448 /* Rescan all devices, after closing previous set - convenience function */
457 * Find all devices that match the criteria, allowing "wildcarding" as well
458 * by allowing NULL or ANY values to match all. The array returned is static
459 * and may be used until the next invocation of deviceFind().
462 deviceFind(char *name, DeviceType class)
464 static Device *found[DEV_MAX];
468 for (i = 0; i < numDevs; i++) {
469 if ((!name || !strcmp(Devices[i]->name, name))
470 && (class == DEVICE_TYPE_ANY || class == Devices[i]->type))
471 found[j++] = Devices[i];
474 return j ? found : NULL;
478 deviceFindDescr(char *name, char *desc, DeviceType class)
480 static Device *found[DEV_MAX];
484 for (i = 0; i < numDevs; i++) {
485 if ((!name || !strcmp(Devices[i]->name, name)) &&
486 (!desc || !strcmp(Devices[i]->description, desc)) &&
487 (class == DEVICE_TYPE_ANY || class == Devices[i]->type))
488 found[j++] = Devices[i];
491 return j ? found : NULL;
495 deviceCount(Device **devs)
501 for (i = 0; devs[i]; i++);
506 * Create a menu listing all the devices of a certain type in the system.
507 * The passed-in menu is expected to be a "prototype" from which the new
511 deviceCreateMenu(DMenu *menu, DeviceType type, int (*hook)(dialogMenuItem *d), int (*check)(dialogMenuItem *d))
518 devs = deviceFind(NULL, type);
519 numdevs = deviceCount(devs);
522 tmp = (DMenu *)safe_malloc(sizeof(DMenu) + (sizeof(dialogMenuItem) * (numdevs + 1)));
523 bcopy(menu, tmp, sizeof(DMenu));
524 for (i = 0; devs[i]; i++) {
525 tmp->items[i].prompt = devs[i]->name;
526 for (j = 0; j < numDevs; j++) {
527 if (devs[i] == Devices[j]) {
528 tmp->items[i].title = Devices[j]->description;
533 tmp->items[i].title = "<unknown device type>";
534 tmp->items[i].fire = hook;
535 tmp->items[i].checked = check;
537 tmp->items[i].title = NULL;