]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.sbin/sysinstall/cdrom.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.sbin / sysinstall / cdrom.c
1 /*
2  * The new sysinstall program.
3  *
4  * This is probably the last attempt in the `sysinstall' line, the next
5  * generation being slated to essentially a complete rewrite.
6  *
7  * $FreeBSD$
8  *
9  * Copyright (c) 1995
10  *      Jordan Hubbard.  All rights reserved.
11  * Copyright (c) 1995
12  *      Gary J Palmer. All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer,
19  *    verbatim and that no modifications are made prior to this
20  *    point in the file.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  */
38
39 /* These routines deal with getting things off of CDROM media */
40
41 #include "sysinstall.h"
42 #include <sys/stat.h>
43 #include <sys/errno.h>
44 #include <sys/param.h>
45 #include <sys/wait.h>
46 #include <unistd.h>
47 #include <grp.h>
48 #include <fcntl.h>
49 #include <libutil.h>
50
51 #define CD9660
52 #include <sys/mount.h>
53 #include <isofs/cd9660/cd9660_mount.h>
54 #undef CD9660
55
56 static Boolean cdromMounted;
57 static Boolean previouslyMounted; /* Was the disc already mounted? */
58 static char mountpoint[MAXPATHLEN] = "/dist";
59 int CDROMInitQuiet;
60
61 static properties
62 read_props(char *name)
63 {
64         int fd;
65         properties n;
66
67         fd = open(name, O_RDONLY);
68         if (fd == -1)
69             return NULL;
70         n = properties_read(fd);
71         close(fd);
72         return n;
73 }
74
75 Boolean
76 mediaInitCDROM(Device *dev)
77 {
78     struct iso_args     args;
79     properties cd_attr = NULL;
80     char *cp = NULL;
81     Boolean readInfo = TRUE;
82     static Boolean bogusCDOK = FALSE;
83     int err;
84
85     if (cdromMounted)
86         return TRUE;
87
88     Mkdir(mountpoint);
89     bzero(&args, sizeof(args));
90     args.fspec = dev->devname;
91     args.flags = 0;
92     err = mount("cd9660", mountpoint, MNT_RDONLY, (caddr_t) &args);
93     /* If disc inserted too recently first access generates EIO, try again */
94     if (err == -1 && errno == EIO)
95         err = mount("cd9660", mountpoint, MNT_RDONLY, (caddr_t) &args);
96     if (err == -1) {
97         if (errno == EINVAL) {
98             msgConfirm("The disc in your drive looks more like an Audio disc than a FreeBSD release.");
99             return FALSE;
100         }
101         if (errno == EBUSY) {
102             /* Perhaps the CDROM drive is already mounted as /cdrom */
103             if (file_readable("/cdrom/cdrom.inf")) {
104                 previouslyMounted = TRUE;
105                 strlcpy(mountpoint, "/cdrom", 7);
106                 errno = 0;
107             }
108         }
109         if (errno) {
110             if (!CDROMInitQuiet)
111                 msgConfirm("Error mounting %s on %s: %s (%u)", dev->devname,
112                     mountpoint, strerror(errno), errno);
113             return FALSE;
114         }
115     }
116     cdromMounted = TRUE;
117
118     if (!file_readable(string_concat(mountpoint, "/cdrom.inf")) && !bogusCDOK) {
119         if (msgYesNo("Warning: The disc currently in the drive is either not a FreeBSD\n"
120                      "disc or it is an older (pre 2.1.5) FreeBSD CD which does not\n"
121                      "have a version number on it.  Do you wish to use this disc anyway?") != 0) {
122             if (!previouslyMounted)
123                 unmount(mountpoint, MNT_FORCE);
124             cdromMounted = FALSE;
125             return FALSE;
126         }
127         else {
128             readInfo = FALSE;
129             bogusCDOK = TRUE;
130         }
131     }
132
133     if (readInfo) {
134         if (!(cd_attr = read_props(string_concat(mountpoint, "/cdrom.inf")))
135             || !(cp = property_find(cd_attr, "CD_VERSION"))) {
136             msgConfirm("Unable to find a %s/cdrom.inf file.\n"
137                        "Either this is not a FreeBSD disc, there is a problem with\n"
138                        "the CDROM driver or something is wrong with your hardware.\n"
139                        "Please fix this problem (check the console logs on VTY2) and\n"
140                        "try again.", mountpoint);
141         }
142         else {
143             if (variable_cmp(VAR_RELNAME, cp) &&
144                 variable_cmp(VAR_RELNAME, "any") &&
145                 variable_cmp(cp, "any") &&
146                 !bogusCDOK) {
147                 msgConfirm("Warning: The version of the FreeBSD disc currently in the drive\n"
148                            "(%s) does not match the version of the boot floppy\n"
149                            "(%s).\n\n"
150                            "If this is intentional, to avoid this message in the future\n"
151                            "please visit the Options editor to set the boot floppy version\n"
152                            "string to match that of the disc before selecting it as your\n"
153                            "installation media.", cp, variable_get(VAR_RELNAME));
154
155                 if (msgYesNo("Would you like to try and use this disc anyway?") != 0) {
156                     if (!previouslyMounted)
157                         unmount(mountpoint, MNT_FORCE);
158                     cdromMounted = FALSE;
159                     properties_free(cd_attr);
160                     return FALSE;
161                 }
162                 else
163                     bogusCDOK = TRUE;
164             }
165             if ((cp = property_find(cd_attr, "CD_MACHINE_ARCH")) != NULL) {
166                 if (strcmp(cp, "any") &&
167 #ifdef __alpha__
168                   strcmp(cp, "alpha")) {
169 #elif defined(PC98)
170                   strcmp(cp, "pc98")) {
171 #elif defined(__sparc64__)
172                   strcmp(cp, "sparc64")) {
173 #else
174                   strcmp(cp, "x86")) {
175 #endif
176                     msgConfirm("Fatal: The FreeBSD install CD/DVD currently in the drive\n"
177                            "is for the %s architecture, not the machine you're using.\n\n"
178
179                            "Please use the correct installation CD/DVD for your machine type.", cp);
180
181                     if (!previouslyMounted)
182                         unmount(mountpoint, MNT_FORCE);
183                     cdromMounted = FALSE;
184                     properties_free(cd_attr);
185                     return FALSE;
186                 }
187             }
188             if ((cp = property_find(cd_attr, "CD_VOLUME")) != NULL) {
189                 dev->volume = atoi(cp);
190                 /* XXX - Sanity check the volume here? */
191                 msgDebug("CD Volume %d initialized!\n", dev->volume);
192             } else {
193                 dev->volume = 0;
194             }
195         }
196     }
197     if (cd_attr)
198         properties_free(cd_attr);
199     return TRUE;
200 }
201
202 FILE *
203 mediaGetCDROM(Device *dev, char *file, Boolean probe)
204 {
205     return mediaGenericGet(mountpoint, file);
206 }
207
208 void
209 mediaShutdownCDROM(Device *dev)
210 {
211     if (!cdromMounted)
212         return;
213
214     if (previouslyMounted) {
215         cdromMounted = FALSE;
216         return;
217     }
218
219     if (unmount(mountpoint, MNT_FORCE) != 0)
220         msgConfirm("Could not unmount the CDROM/DVD from %s: %s", mountpoint, strerror(errno));
221     else
222         cdromMounted = FALSE;
223 }