Update sysinstall's NFS module:
[dragonfly.git] / release / sysinstall / kget.c
1 /*-
2  * Copyright (c) 1999 Andrzej Bialecki <abial@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/release/sysinstall/kget.c,v 1.14.2.1 2000/07/14 10:21:32 jhb Exp $
27  * $DragonFly: src/release/sysinstall/Attic/kget.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
28  */
29
30 #ifdef __alpha__
31 int
32 kget(char *out)
33 {
34     return -1;
35 }
36
37 #else
38
39 #include "sysinstall.h"
40 #include <sys/sysctl.h>
41 #include <machine/uc_device.h>
42
43 int
44 kget(char *out)
45 {
46     int len, i, bytes_written = 0;
47     char *buf;
48     char *mib1 = "machdep.uc_devlist";
49     char name[9];
50     FILE *fout = NULL;
51     struct uc_device *id;
52     char *p;
53  
54     /* create the output file; if we end up not writing to it, we'll 
55        unlink() it later. */
56     fout = fopen(out, "w");
57     if (fout == NULL) {
58         msgDebug("kget: Unable to open %s for writing.\n", out);
59         return -1;
60     }
61
62     /* We use sysctlbyname, because the oid is unknown (OID_AUTO) */
63     /* get the buffer size */
64     i = sysctlbyname(mib1, NULL, &len, NULL, NULL);
65     if (i) {
66         msgDebug("kget: error buffer sizing\n");
67         goto bail;
68     }
69     if (len <= 0) {
70         msgDebug("kget: mib1 has length of %d\n", len);
71         goto bail;
72     }
73     buf = (char *)alloca(len * sizeof(char));
74     i = sysctlbyname(mib1, buf, &len, NULL, NULL);
75     if (i) {
76         msgDebug("kget: error retrieving data\n");
77         goto bail;
78     }
79
80
81     i = 0;
82     while (i < len) {
83         id = (struct uc_device *)(buf + i);
84         p = (buf + i + sizeof(struct uc_device));
85         strncpy(name, p, 8);
86         if (!id->id_enabled) {
87             bytes_written += fprintf(fout, "di %s%d\n", name, id->id_unit);
88         }
89         else {
90             bytes_written += fprintf(fout, "en %s%d\n", name, id->id_unit);
91             if (id->id_iobase > 0) {
92                 bytes_written += fprintf(fout, "po %s%d %#x\n",
93                                          name, id->id_unit, id->id_iobase);
94             }
95             if (id->id_irq > 0) {
96                 bytes_written += fprintf(fout, "ir %s%d %d\n", name,
97                                          id->id_unit, ffs(id->id_irq) - 1);
98             }
99             if (id->id_drq > 0) {
100                 bytes_written += fprintf(fout, "dr %s%d %d\n", name,
101                                          id->id_unit, id->id_drq);
102             }
103             if (id->id_maddr > 0) {
104                 bytes_written += fprintf(fout, "iom %s%d %#x\n", name,
105                                          id->id_unit, (u_int)id->id_maddr);
106             }
107             if (id->id_msize > 0) {
108                 bytes_written += fprintf(fout, "ios %s%d %d\n", name,
109                                          id->id_unit, id->id_msize);
110             }
111             bytes_written += fprintf(fout, "f %s%d %#x\n", name,
112                                      id->id_unit, id->id_flags);
113         }
114         i += sizeof(struct uc_device) + 8;
115     }
116
117 bail:
118     if (bytes_written)
119         fprintf(fout, "q\n");
120     else
121         unlink(out);
122     fclose(fout);
123     return 0;
124 }
125
126 #endif  /* !alpha */