Initial import from FreeBSD RELENG_4:
[dragonfly.git] / release / 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: src/release/sysinstall/cdrom.c,v 1.47.2.5 2002/10/24 12:56:06 nyan Exp $
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
60 static properties
61 read_props(char *name)
62 {
63         int fd;
64         properties n;
65
66         fd = open(name, O_RDONLY);
67         if (fd == -1)
68             return NULL;
69         n = properties_read(fd);
70         close(fd);
71         return n;
72 }
73
74 Boolean
75 mediaInitCDROM(Device *dev)
76 {
77     struct iso_args     args;
78     properties cd_attr = NULL;
79     char *cp = NULL;
80     Boolean readInfo = TRUE;
81     static Boolean bogusCDOK = FALSE;
82
83     if (cdromMounted)
84         return TRUE;
85
86     Mkdir(mountpoint);
87     bzero(&args, sizeof(args));
88     args.fspec = dev->devname;
89     args.flags = 0;
90     if (mount("cd9660", mountpoint, MNT_RDONLY, (caddr_t) &args) == -1) {
91         if (errno == EINVAL) {
92             msgConfirm("The disc in your drive looks more like an Audio disc than a FreeBSD release.");
93             return FALSE;
94         } else if (errno == EBUSY) {
95             /* Perhaps the CDROM drive is already mounted as /cdrom */
96             if (file_readable("/cdrom/cdrom.inf")) {
97                 previouslyMounted = TRUE;
98                 strlcpy(mountpoint, "/cdrom", 7);
99             }
100         } else {
101             msgConfirm("Error mounting %s on %s: %s (%u)", dev->devname, mountpoint, strerror(errno), errno);
102             return FALSE;
103         }
104     }
105     cdromMounted = TRUE;
106
107     if (!file_readable(string_concat(mountpoint, "/cdrom.inf")) && !bogusCDOK) {
108         if (msgYesNo("Warning: The disc currently in the drive is either not a FreeBSD\n"
109                      "disc or it is an older (pre 2.1.5) FreeBSD CD which does not\n"
110                      "have a version number on it.  Do you wish to use this disc anyway?") != 0) {
111             if (!previouslyMounted)
112                 unmount(mountpoint, MNT_FORCE);
113             cdromMounted = FALSE;
114             return FALSE;
115         }
116         else {
117             readInfo = FALSE;
118             bogusCDOK = TRUE;
119         }
120     }
121
122     if (readInfo) {
123         if (!(cd_attr = read_props(string_concat(mountpoint, "/cdrom.inf")))
124             || !(cp = property_find(cd_attr, "CD_VERSION"))) {
125             msgConfirm("Unable to find a %s/cdrom.inf file.\n"
126                        "Either this is not a FreeBSD disc, there is a problem with\n"
127                        "the CDROM driver or something is wrong with your hardware.\n"
128                        "Please fix this problem (check the console logs on VTY2) and\n"
129                        "try again.", mountpoint);
130         }
131         else {
132             if (variable_cmp(VAR_RELNAME, cp) &&
133                 variable_cmp(VAR_RELNAME, "any") &&
134                 variable_cmp(cp, "any") &&
135                 !bogusCDOK) {
136                 msgConfirm("Warning: The version of the FreeBSD disc currently in the drive\n"
137                            "(%s) does not match the version of the boot floppy\n"
138                            "(%s).\n\n"
139                            "If this is intentional, to avoid this message in the future\n"
140                            "please visit the Options editor to set the boot floppy version\n"
141                            "string to match that of the disc before selecting it as your\n"
142                            "installation media.", cp, variable_get(VAR_RELNAME));
143
144                 if (msgYesNo("Would you like to try and use this disc anyway?") != 0) {
145                     if (!previouslyMounted)
146                         unmount(mountpoint, MNT_FORCE);
147                     cdromMounted = FALSE;
148                     properties_free(cd_attr);
149                     return FALSE;
150                 }
151                 else
152                     bogusCDOK = TRUE;
153             }
154             if ((cp = property_find(cd_attr, "CD_MACHINE_ARCH")) != NULL) {
155                 if (strcmp(cp, "any") &&
156 #ifdef __alpha__
157                   strcmp(cp, "alpha")) {
158 #elif defined(PC98)
159                   strcmp(cp, "pc98")) {
160 #else
161                   strcmp(cp, "x86")) {
162 #endif
163                     msgConfirm("Fatal: The FreeBSD install CD/DVD currently in the drive\n"
164                            "is for the %s architecture, not the machine you're using.\n\n"
165
166                            "Please use the correct installation CD/DVD for your machine type.", cp);
167
168                     if (!previouslyMounted)
169                         unmount(mountpoint, MNT_FORCE);
170                     cdromMounted = FALSE;
171                     properties_free(cd_attr);
172                     return FALSE;
173                 }
174             }
175             if ((cp = property_find(cd_attr, "CD_VOLUME")) != NULL) {
176                 dev->volume = atoi(cp);
177                 /* XXX - Sanity check the volume here? */
178                 msgDebug("CD Volume %d initialized!\n", dev->volume);
179             } else {
180                 dev->volume = 0;
181             }
182         }
183     }
184     if (cd_attr)
185         properties_free(cd_attr);
186     return TRUE;
187 }
188
189 FILE *
190 mediaGetCDROM(Device *dev, char *file, Boolean probe)
191 {
192     return mediaGenericGet(mountpoint, file);
193 }
194
195 void
196 mediaShutdownCDROM(Device *dev)
197 {
198     if (!cdromMounted)
199         return;
200
201     if (previouslyMounted) {
202         cdromMounted = FALSE;
203         return;
204     }
205
206     if (unmount(mountpoint, MNT_FORCE) != 0)
207         msgConfirm("Could not unmount the CDROM/DVD from %s: %s", mountpoint, strerror(errno));
208     else
209         cdromMounted = FALSE;
210 }