1 /* $NetBSD: md.c,v 1.7 2022/01/29 16:01:20 martin Exp $ */
2
3 /*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
6 *
7 * Based on code written by Philip A. Nelson for Piermont Information
8 * Systems Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19 * or promote products derived from this software without specific prior
20 * written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /* md.c -- shark machine specific routines */
36
37 #include <stdio.h>
38 #include <curses.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <util.h>
42 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 #include <sys/param.h>
45
46 #include "defs.h"
47 #include "md.h"
48 #include "mbr.h"
49 #include "msg_defs.h"
50 #include "menu_defs.h"
51
52 static int clear_mbr(const char *, char *, size_t);
53
54 void
md_init(void)55 md_init(void)
56 {
57 }
58
59 void
md_init_set_status(int flags)60 md_init_set_status(int flags)
61 {
62 (void)flags;
63 }
64
65 bool
md_get_info(struct install_partition_desc * install)66 md_get_info(struct install_partition_desc *install)
67 {
68 struct disklabel disklabel;
69 int fd;
70 char dev_name[100];
71
72 snprintf(dev_name, 100, "/dev/r%sc", pm->diskdev);
73
74 fd = open(dev_name, O_RDONLY, 0);
75 if (fd < 0) {
76 endwin();
77 fprintf(stderr, "Can't open %s\n", dev_name);
78 exit(1);
79 }
80 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
81 endwin();
82 fprintf(stderr, "Can't read disklabel on %s.\n", dev_name);
83 close(fd);
84 exit(1);
85 }
86 close(fd);
87
88 pm->dlcyl = disklabel.d_ncylinders;
89 pm->dlhead = disklabel.d_ntracks;
90 pm->dlsec = disklabel.d_nsectors;
91 pm->sectorsize = disklabel.d_secsize;
92 pm->dlcylsize = disklabel.d_secpercyl;
93
94 /*
95 * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec)
96 * and secperunit, just in case the disk is already labelled.
97 * (If our new label's RAW_PART size ends up smaller than the
98 * in-core RAW_PART size value, updating the label will fail.)
99 */
100 pm->dlsize = pm->dlcyl*pm->dlhead*pm->dlsec;
101 if (disklabel.d_secperunit > pm->dlsize)
102 pm->dlsize = disklabel.d_secperunit;
103
104 return true;
105 }
106
107 /*
108 * md back-end code for menu-driven BSD disklabel editor.
109 */
110 int
md_make_bsd_partitions(struct install_partition_desc * install)111 md_make_bsd_partitions(struct install_partition_desc *install)
112 {
113 return make_bsd_partitions(install);
114 }
115
116 /*
117 * any additional partition validation
118 */
119 bool
md_check_partitions(struct install_partition_desc * install)120 md_check_partitions(struct install_partition_desc *install)
121 {
122 return true;
123 }
124
125 /*
126 * hook called before writing new disklabel.
127 */
128 bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)129 md_pre_disklabel(struct install_partition_desc *install,
130 struct disk_partitions *parts)
131 {
132 char diskpath[MAXPATHLEN];
133
134 if (clear_mbr(pm->diskdev, diskpath, sizeof(diskpath)) == -1) {
135 msg_fmt_display(MSG_badclearmbr, "%s", diskpath);
136 process_menu(MENU_ok, NULL);
137 }
138
139 return true;
140 }
141
142 /*
143 * hook called after writing disklabel to new target disk.
144 */
145 bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)146 md_post_disklabel(struct install_partition_desc *install,
147 struct disk_partitions *parts)
148 {
149 return true;
150 }
151
152 /*
153 * hook called after upgrade() or install() has finished setting
154 * up the target disk but immediately before the user is given the
155 * ``disks are now set up'' message.
156 */
157 int
md_post_newfs(struct install_partition_desc * install)158 md_post_newfs(struct install_partition_desc *install)
159 {
160 return 0;
161 }
162
163 int
md_post_extract(struct install_partition_desc * install,bool upgrade)164 md_post_extract(struct install_partition_desc *install, bool upgrade)
165 {
166 msg_display(MSG_setbootdevice);
167 process_menu(MENU_ok, NULL);
168
169 return 0;
170 }
171
172 void
md_cleanup_install(struct install_partition_desc * install)173 md_cleanup_install(struct install_partition_desc *install)
174 {
175 #ifndef DEBUG
176 enable_rc_conf();
177 add_rc_conf("wscons=YES\n");
178
179 /* Configure a single screen. */
180 run_program(RUN_CHROOT,
181 "sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;"
182 "H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf");
183
184 #endif
185 }
186
187 int
md_pre_update(struct install_partition_desc * install)188 md_pre_update(struct install_partition_desc *install)
189 {
190 return 1;
191 }
192
193 /* Upgrade support */
194 int
md_update(struct install_partition_desc * install)195 md_update(struct install_partition_desc *install)
196 {
197 md_post_newfs(install);
198 return 1;
199 }
200
201 /*
202 * Clears the disk's first sector by writing all zeros over it.
203 * Returns 0 on success, -1 on failure. Leaves the disk's path in
204 * the output diskpath buffer for further usage in error messages.
205 */
206 static int
clear_mbr(const char * disk,char * diskpath,size_t diskpathlen)207 clear_mbr(const char *disk, char *diskpath, size_t diskpathlen)
208 {
209 int fd;
210 char sector[512];
211
212 fd = opendisk(disk, O_WRONLY, diskpath, diskpathlen, 0);
213 if (fd < 0)
214 return -1;
215
216 memset(sector, 0, sizeof(sector));
217 if (pwrite(fd, §or, sizeof(sector), 0) < 0) {
218 close(fd);
219 return -1;
220 }
221
222 close(fd);
223 return 0;
224 }
225
226 int
md_pre_mount(struct install_partition_desc * install,size_t ndx)227 md_pre_mount(struct install_partition_desc *install, size_t ndx)
228 {
229 return 0;
230 }
231
232 /* returns false if no write-back of parts is required */
233 bool
md_mbr_update_check(struct disk_partitions * parts,mbr_info_t * mbri)234 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
235 {
236 return false;
237 }
238
239 #ifdef HAVE_GPT
240 /*
241 * New GPT partitions have been written, update bootloader or remember
242 * data untill needed in md_post_newfs
243 */
244 bool
md_gpt_post_write(struct disk_partitions * parts,part_id root_id,bool root_is_new,part_id efi_id,bool efi_is_new)245 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
246 bool root_is_new, part_id efi_id, bool efi_is_new)
247 {
248 return true;
249 }
250 #endif
251
252 bool
md_parts_use_wholedisk(struct disk_partitions * parts)253 md_parts_use_wholedisk(struct disk_partitions *parts)
254 {
255 return parts_use_wholedisk(parts, 0, NULL);
256 }
257