Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sys / i386 / i386 / autoconf.c
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * William Jolitz.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      from: @(#)autoconf.c    7.1 (Berkeley) 5/9/91
37  * $FreeBSD: src/sys/i386/i386/autoconf.c,v 1.146.2.2 2001/06/07 06:05:58 dd Exp $
38  * $DragonFly: src/sys/i386/i386/Attic/autoconf.c,v 1.27 2006/09/10 01:26:38 dillon Exp $
39  */
40
41 /*
42  * Setup the system to run on the current machine.
43  *
44  * Configure() is called at boot time and initializes the vba
45  * device tables and the memory controller monitoring.  Available
46  * devices are determined (from possibilities mentioned in ioconf.c),
47  * and the drivers are initialized.
48  */
49 #include "opt_bootp.h"
50 #include "opt_ffs.h"
51 #include "opt_cd9660.h"
52 #include "opt_nfs.h"
53 #include "opt_nfsroot.h"
54 #include "opt_bus.h"
55 #include "opt_rootdevname.h"
56
57 #include "use_isa.h"
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/bootmaj.h>
62 #include <sys/bus.h>
63 #include <sys/conf.h>
64 #include <sys/disklabel.h>
65 #include <sys/diskslice.h>
66 #include <sys/reboot.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/mount.h>
70 #include <sys/cons.h>
71 #include <sys/thread.h>
72 #include <sys/device.h>
73 #include <sys/machintr.h>
74
75 #include <machine/bootinfo.h>
76 #include <machine/ipl.h>
77 #include <machine/md_var.h>
78 #include <machine/smp.h>
79 #include <i386/icu/icu.h>
80
81 #include <machine/pcb.h>
82 #include <machine/pcb_ext.h>
83 #include <machine/vm86.h>
84 #include <machine/globaldata.h>
85
86 #if NISA > 0
87 #include <bus/isa/isavar.h>
88
89 device_t isa_bus_device = 0;
90 #endif
91
92 static void     configure_first (void *);
93 static void     configure (void *);
94 static void     configure_final (void *);
95
96 #if defined(FFS) && defined(FFS_ROOT)
97 static void     setroot (void);
98 #endif
99
100 #if defined(NFS) && defined(NFS_ROOT)
101 #if !defined(BOOTP_NFSROOT)
102 static void     pxe_setup_nfsdiskless(void);
103 #endif
104 #endif
105
106 SYSINIT(configure1, SI_SUB_CONFIGURE, SI_ORDER_FIRST, configure_first, NULL);
107 /* SI_ORDER_SECOND is hookable */
108 SYSINIT(configure2, SI_SUB_CONFIGURE, SI_ORDER_THIRD, configure, NULL);
109 /* SI_ORDER_MIDDLE is hookable */
110 SYSINIT(configure3, SI_SUB_CONFIGURE, SI_ORDER_ANY, configure_final, NULL);
111
112 cdev_t  rootdev = NOCDEV;
113 cdev_t  dumpdev = NOCDEV;
114
115 /*
116  * Determine i/o configuration for a machine.
117  */
118 static void
119 configure_first(dummy)
120         void *dummy;
121 {
122 }
123
124 static void
125 configure(dummy)
126         void *dummy;
127 {
128         /*
129          * Final interrupt support acviation, then enable hardware interrupts.
130          */
131         MachIntrABI.finalize();
132         cpu_enable_intr();
133
134         /*
135          * This will configure all devices, generally starting with the
136          * nexus (i386/i386/nexus.c).  The nexus ISA code explicitly
137          * dummies up the attach in order to delay legacy initialization
138          * until after all other busses/subsystems have had a chance
139          * at those resources.
140          */
141         root_bus_configure();
142
143 #if NISA > 0
144         /*
145          * Explicitly probe and attach ISA last.  The isa bus saves
146          * it's device node at attach time for us here.
147          */
148         if (isa_bus_device)
149                 isa_probe_children(isa_bus_device);
150 #endif
151
152         /*
153          * Allow lowering of the ipl to the lowest kernel level if we
154          * panic (or call tsleep() before clearing `cold').  No level is
155          * completely safe (since a panic may occur in a critical region
156          * at splhigh()), but we want at least bio interrupts to work.
157          */
158         safepri = TDPRI_KERN_USER;
159 }
160
161 static void
162 configure_final(dummy)
163         void *dummy;
164 {
165         int i;
166
167         cninit_finish();
168
169         if (bootverbose) {
170 #ifdef APIC_IO
171                 imen_dump();
172 #endif /* APIC_IO */
173
174                 /*
175                  * Print out the BIOS's idea of the disk geometries.
176                  */
177                 printf("BIOS Geometries:\n");
178                 for (i = 0; i < N_BIOS_GEOM; i++) {
179                         unsigned long bios_geom;
180                         int max_cylinder, max_head, max_sector;
181
182                         bios_geom = bootinfo.bi_bios_geom[i];
183
184                         /*
185                          * XXX the bootstrap punts a 1200K floppy geometry
186                          * when the get-disk-geometry interrupt fails.  Skip
187                          * drives that have this geometry.
188                          */
189                         if (bios_geom == 0x4f010f)
190                                 continue;
191
192                         printf(" %x:%08lx ", i, bios_geom);
193                         max_cylinder = bios_geom >> 16;
194                         max_head = (bios_geom >> 8) & 0xff;
195                         max_sector = bios_geom & 0xff;
196                         printf(
197                 "0..%d=%d cylinders, 0..%d=%d heads, 1..%d=%d sectors\n",
198                                max_cylinder, max_cylinder + 1,
199                                max_head, max_head + 1,
200                                max_sector, max_sector);
201                 }
202                 printf(" %d accounted for\n", bootinfo.bi_n_bios_used);
203
204                 printf("Device configuration finished.\n");
205         }
206         cold = 0;
207 }
208
209 #ifdef BOOTP
210 void bootpc_init(void);
211 #endif
212 /*
213  * Do legacy root filesystem discovery.
214  */
215 void
216 cpu_rootconf()
217 {
218 #ifdef BOOTP
219         bootpc_init();
220 #endif
221 #if defined(NFS) && defined(NFS_ROOT)
222 #if !defined(BOOTP_NFSROOT)
223         pxe_setup_nfsdiskless();
224         if (nfs_diskless_valid)
225 #endif
226                 rootdevnames[0] = "nfs:";
227 #endif
228 #if defined(FFS) && defined(FFS_ROOT)
229         if (!rootdevnames[0])
230                 setroot();
231 #endif
232 }
233 SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL)
234
235 u_long  bootdev = 0;            /* not a cdev_t - encoding is different */
236
237 #if defined(FFS) && defined(FFS_ROOT)
238 #define FDMAJOR         2
239 #define FDUNITSHIFT     6
240
241 /*
242  * The boot code uses old block device major numbers to pass bootdev to
243  * us.  We have to translate these to character device majors because
244  * we don't have block devices any more.
245  */
246 static int
247 boot_translate_majdev(int bmajor)
248 {
249         static int conv[] = { BOOTMAJOR_CONVARY };
250
251         if (bmajor >= 0 && bmajor < sizeof(conv)/sizeof(conv[0]))
252                 return(conv[bmajor]);
253         return(-1);
254 }
255
256 /*
257  * Attempt to find the device from which we were booted.
258  * If we can do so, and not instructed not to do so,
259  * set rootdevs[] and rootdevnames[] to correspond to the
260  * boot device(s).
261  *
262  * This code survives in order to allow the system to be 
263  * booted from legacy environments that do not correctly
264  * populate the kernel environment. There are significant
265  * restrictions on the bootability of the system in this
266  * situation; it can only be mounting root from a 'da'
267  * 'wd' or 'fd' device, and the root filesystem must be ufs.
268  */
269 static void
270 setroot()
271 {
272         int majdev, mindev, unit, slice, part;
273         cdev_t newrootdev, dev;
274         char partname[2];
275         char *sname;
276
277         if ((bootdev & B_MAGICMASK) != B_DEVMAGIC) {
278                 printf("no B_DEVMAGIC (bootdev=%#lx)\n", bootdev);
279                 return;
280         }
281         majdev = boot_translate_majdev(B_TYPE(bootdev));
282         if (bootverbose) {
283                 printf("bootdev: %08lx type=%ld unit=%ld "
284                         "slice=%ld part=%ld major=%d\n",
285                         bootdev, B_TYPE(bootdev), B_UNIT(bootdev),
286                         B_SLICE(bootdev), B_PARTITION(bootdev), majdev);
287         }
288         dev = udev2dev(makeudev(majdev, 0), 0);
289         if (!dev_is_good(dev))
290                 return;
291         unit = B_UNIT(bootdev);
292         slice = B_SLICE(bootdev);
293         if (slice == WHOLE_DISK_SLICE)
294                 slice = COMPATIBILITY_SLICE;
295         if (slice < 0 || slice >= MAX_SLICES) {
296                 printf("bad slice\n");
297                 return;
298         }
299
300         /*
301          * XXX kludge for inconsistent unit numbering and lack of slice
302          * support for floppies.
303          */
304         if (majdev == FD_CDEV_MAJOR) {
305                 slice = COMPATIBILITY_SLICE;
306                 part = RAW_PART;
307                 mindev = unit << FDUNITSHIFT;
308         } else {
309                 part = B_PARTITION(bootdev);
310                 mindev = dkmakeminor(unit, slice, part);
311         }
312         newrootdev = udev2dev(makeudev(majdev, mindev), 0);
313         if (!dev_is_good(newrootdev))
314                 return;
315         sname = dsname(newrootdev, unit, slice, part, partname);
316         rootdevnames[0] = kmalloc(strlen(sname) + 6, M_DEVBUF, M_WAITOK);
317         sprintf(rootdevnames[0], "ufs:%s%s", sname, partname);
318
319         /*
320          * For properly dangerously dedicated disks (ones with a historical
321          * bogus partition table), the boot blocks will give slice = 4, but
322          * the kernel will only provide the compatibility slice since it
323          * knows that slice 4 is not a real slice.  Arrange to try mounting
324          * the compatibility slice as root if mounting the slice passed by
325          * the boot blocks fails.  This handles the dangerously dedicated
326          * case and perhaps others.
327          */
328         if (slice == COMPATIBILITY_SLICE)
329                 return;
330         slice = COMPATIBILITY_SLICE;
331         sname = dsname(newrootdev, unit, slice, part, partname);
332         rootdevnames[1] = kmalloc(strlen(sname) + 6, M_DEVBUF, M_WAITOK);
333         sprintf(rootdevnames[1], "ufs:%s%s", sname, partname);
334 }
335 #endif
336
337 #if defined(NFS) && defined(NFS_ROOT)
338 #if !defined(BOOTP_NFSROOT)
339
340 #include <sys/socket.h>
341 #include <net/if.h>
342 #include <net/if_dl.h>
343 #include <net/if_types.h>
344 #include <net/if_var.h>
345 #include <net/ethernet.h>
346 #include <netinet/in.h>
347 #include <vfs/nfs/rpcv2.h>
348 #include <vfs/nfs/nfsproto.h>
349 #include <vfs/nfs/nfs.h>
350 #include <vfs/nfs/nfsdiskless.h>
351
352 extern struct nfs_diskless      nfs_diskless;
353
354 /*
355  * Convert a kenv variable to a sockaddr.  If the kenv variable does not
356  * exist the sockaddr will remain zerod out (callers typically just check
357  * sin_len).  A network address of 0.0.0.0 is equivalent to failure.
358  */
359 static int
360 inaddr_to_sockaddr(char *ev, struct sockaddr_in *sa)
361 {
362         u_int32_t       a[4];
363         char            *cp;
364
365         bzero(sa, sizeof(*sa));
366
367         if ((cp = kgetenv(ev)) == NULL)
368                 return(1);
369         if (sscanf(cp, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]) != 4)
370                 return(1);
371         if (a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0)
372                 return(1);
373         /* XXX is this ordering correct? */
374         sa->sin_addr.s_addr = (a[3] << 24) + (a[2] << 16) + (a[1] << 8) + a[0];
375         sa->sin_len = sizeof(*sa);
376         sa->sin_family = AF_INET;
377         return(0);
378 }
379
380 static int
381 hwaddr_to_sockaddr(char *ev, struct sockaddr_dl *sa)
382 {
383         char            *cp;
384         u_int32_t       a[6];
385
386         bzero(sa, sizeof(*sa));
387         sa->sdl_len = sizeof(*sa);
388         sa->sdl_family = AF_LINK;
389         sa->sdl_type = IFT_ETHER;
390         sa->sdl_alen = ETHER_ADDR_LEN;
391         if ((cp = kgetenv(ev)) == NULL)
392                 return(1);
393         if (sscanf(cp, "%x:%x:%x:%x:%x:%x", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5]) != 6)
394                 return(1);
395         sa->sdl_data[0] = a[0];
396         sa->sdl_data[1] = a[1];
397         sa->sdl_data[2] = a[2];
398         sa->sdl_data[3] = a[3];
399         sa->sdl_data[4] = a[4];
400         sa->sdl_data[5] = a[5];
401         return(0);
402 }
403
404 static int
405 decode_nfshandle(char *ev, u_char *fh) 
406 {
407         u_char  *cp;
408         int     len, val;
409
410         if (((cp = kgetenv(ev)) == NULL) || (strlen(cp) < 2) || (*cp != 'X'))
411                 return(0);
412         len = 0;
413         cp++;
414         for (;;) {
415                 if (*cp == 'X')
416                         return(len);
417                 if ((sscanf(cp, "%2x", &val) != 1) || (val > 0xff))
418                         return(0);
419                 *(fh++) = val;
420                 len++;
421                 cp += 2;
422                 if (len > NFSX_V2FH)
423                     return(0);
424         }
425 }
426
427 /*
428  * Populate the essential fields in the nfsv3_diskless structure.
429  *
430  * The loader is expected to export the following environment variables:
431  *
432  * boot.netif.ip                IP address on boot interface
433  * boot.netif.netmask           netmask on boot interface
434  * boot.netif.gateway           default gateway (optional)
435  * boot.netif.hwaddr            hardware address of boot interface
436  * boot.nfsroot.server          IP address of root filesystem server
437  * boot.nfsroot.path            path of the root filesystem on server
438  * boot.nfsroot.nfshandle       NFS handle for root filesystem on server
439  */
440 static void
441 pxe_setup_nfsdiskless()
442 {
443         struct nfs_diskless     *nd = &nfs_diskless;
444         struct ifnet            *ifp;
445         struct ifaddr           *ifa;
446         struct sockaddr_dl      *sdl, ourdl;
447         struct sockaddr_in      myaddr, netmask;
448         char                    *cp;
449
450         /* set up interface */
451         if (inaddr_to_sockaddr("boot.netif.ip", &myaddr))
452                 return;
453         if (inaddr_to_sockaddr("boot.netif.netmask", &netmask)) {
454                 printf("PXE: no netmask\n");
455                 return;
456         }
457         bcopy(&myaddr, &nd->myif.ifra_addr, sizeof(myaddr));
458         bcopy(&myaddr, &nd->myif.ifra_broadaddr, sizeof(myaddr));
459         ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr =
460                 myaddr.sin_addr.s_addr | ~ netmask.sin_addr.s_addr;
461         bcopy(&netmask, &nd->myif.ifra_mask, sizeof(netmask));
462
463         if (hwaddr_to_sockaddr("boot.netif.hwaddr", &ourdl)) {
464                 printf("PXE: no hardware address\n");
465                 return;
466         }
467         ifa = NULL;
468         ifp = TAILQ_FIRST(&ifnet);
469         TAILQ_FOREACH(ifp, &ifnet, if_link) {
470                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
471                         if ((ifa->ifa_addr->sa_family == AF_LINK) &&
472                             (sdl = ((struct sockaddr_dl *)ifa->ifa_addr))) {
473                                 if ((sdl->sdl_type == ourdl.sdl_type) &&
474                                     (sdl->sdl_alen == ourdl.sdl_alen) &&
475                                     !bcmp(sdl->sdl_data + sdl->sdl_nlen,
476                                           ourdl.sdl_data + ourdl.sdl_nlen, 
477                                           sdl->sdl_alen))
478                                     goto match_done;
479                         }
480                 }
481         }
482         printf("PXE: no interface\n");
483         return; /* no matching interface */
484 match_done:
485         strlcpy(nd->myif.ifra_name, ifp->if_xname, sizeof(nd->myif.ifra_name));
486         
487         /* set up gateway */
488         inaddr_to_sockaddr("boot.netif.gateway", &nd->mygateway);
489
490         /* XXX set up swap? */
491
492         /* set up root mount */
493         nd->root_args.rsize = 8192;             /* XXX tunable? */
494         nd->root_args.wsize = 8192;
495         nd->root_args.sotype = SOCK_DGRAM;
496         nd->root_args.flags = (NFSMNT_WSIZE | NFSMNT_RSIZE | NFSMNT_RESVPORT);
497         if (inaddr_to_sockaddr("boot.nfsroot.server", &nd->root_saddr)) {
498                 printf("PXE: no server\n");
499                 return;
500         }
501         nd->root_saddr.sin_port = htons(NFS_PORT);
502
503         /*
504          * A tftp-only loader may pass NFS path information without a 
505          * root handle.  Generate a warning but continue configuring.
506          */
507         if (decode_nfshandle("boot.nfsroot.nfshandle", &nd->root_fh[0]) == 0) {
508                 printf("PXE: Warning, no NFS handle passed from loader\n");
509         }
510         if ((cp = kgetenv("boot.nfsroot.path")) != NULL)
511                 strncpy(nd->root_hostnam, cp, MNAMELEN - 1);
512
513         nfs_diskless_valid = 1;
514 }
515
516 #endif
517 #endif