md.c revision 1.2.6.2 1 /* $NetBSD: md.c,v 1.2.6.2 2014/08/20 00:05:15 tls 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 -- hpcarm machine specific routines */
36
37 #include <stdio.h>
38 #include <util.h>
39 #include <sys/param.h>
40 #include <machine/cpu.h>
41 #include <sys/sysctl.h>
42
43 #include "defs.h"
44 #include "md.h"
45 #include "msg_defs.h"
46 #include "menu_defs.h"
47 #include "endian.h"
48 #include "mbr.h"
49
50 void
51 md_init(void)
52 {
53 }
54
55 void
56 md_init_set_status(int flags)
57 {
58 static const struct {
59 const char *name;
60 const int set;
61 } kern_sets[] = {
62 { "IPAQ", SET_KERNEL_IPAQ },
63 { "JORNADA720", SET_KERNEL_JORNADA720 },
64 { "WZERO3", SET_KERNEL_WZERO3 }
65 };
66 static const int mib[2] = {CTL_KERN, KERN_VERSION};
67 size_t len;
68 char *version;
69 u_int i;
70
71 /* check INSTALL kernel name to select an appropriate kernel set */
72 /* XXX: hw.cpu_model has a processor name on arm ports */
73 sysctl(mib, 2, NULL, &len, NULL, 0);
74 version = malloc(len);
75 if (version == NULL)
76 return;
77 sysctl(mib, 2, version, &len, NULL, 0);
78 for (i = 0; i < __arraycount(kern_sets); i++) {
79 if (strstr(version, kern_sets[i].name) != NULL) {
80 set_kernel_set(kern_sets[i].set);
81 break;
82 }
83 }
84 free(version);
85 }
86
87 int
88 md_get_info(void)
89 {
90 return set_bios_geom_with_mbr_guess();
91 }
92
93 /*
94 * md back-end code for menu-driven BSD disklabel editor.
95 */
96 int
97 md_make_bsd_partitions(void)
98 {
99 return make_bsd_partitions();
100 }
101
102 /*
103 * any additional partition validation
104 */
105 int
106 md_check_partitions(void)
107 {
108 return 1;
109 }
110
111 /*
112 * hook called before writing new disklabel.
113 */
114 int
115 md_pre_disklabel(void)
116 {
117 msg_display(MSG_dofdisk);
118
119 /* write edited MBR onto disk. */
120 if (write_mbr(pm->diskdev, &mbr, 1) != 0) {
121 msg_display(MSG_wmbrfail);
122 process_menu(MENU_ok, NULL);
123 return 1;
124 }
125 return 0;
126 }
127
128 /*
129 * hook called after writing disklabel to new target disk.
130 */
131 int
132 md_post_disklabel(void)
133 {
134 /* Sector forwarding / badblocks ... */
135 if (*pm->doessf) {
136 msg_display(MSG_dobad144);
137 return run_program(RUN_DISPLAY, "/usr/sbin/bad144 %s 0",
138 pm->diskdev);
139 }
140 return 0;
141 }
142
143 /*
144 * hook called after upgrade() or install() has finished setting
145 * up the target disk but immediately before the user is given the
146 * ``disks are now set up'' message.
147 */
148 int
149 md_post_newfs(void)
150 {
151 struct mbr_sector pbr;
152 char adevname[STRSIZE];
153 ssize_t sz;
154 int fd = -1;
155
156 snprintf(adevname, sizeof(adevname), "/dev/r%sa", pm->diskdev);
157 fd = open(adevname, O_RDWR);
158 if (fd < 0)
159 goto out;
160
161 /* Read partition boot record */
162 sz = pread(fd, &pbr, sizeof(pbr), 0);
163 if (sz != sizeof(pbr))
164 goto out;
165
166 /* Check magic number */
167 if (pbr.mbr_magic != le16toh(MBR_MAGIC))
168 goto out;
169
170 #define OSNAME "NetBSD60"
171 /* Update oemname */
172 memcpy(&pbr.mbr_oemname, OSNAME, sizeof(OSNAME) - 1);
173
174 /* Clear BPB */
175 memset(&pbr.mbr_bpb, 0, sizeof(pbr.mbr_bpb));
176
177 /* write-backed new patition boot record */
178 (void)pwrite(fd, &pbr, sizeof(pbr), 0);
179
180 out:
181 if (fd >= 0)
182 close(fd);
183 return 0;
184 }
185
186 int
187 md_post_extract(void)
188 {
189 return 0;
190 }
191
192 void
193 md_cleanup_install(void)
194 {
195 #ifndef DEBUG
196 enable_rc_conf();
197 #endif
198 }
199
200 int
201 md_pre_update(void)
202 {
203 return 1;
204 }
205
206 /* Upgrade support */
207 int
208 md_update(void)
209 {
210 md_post_newfs();
211 return 1;
212 }
213
214 int
215 md_check_mbr(mbr_info_t *mbri)
216 {
217 return 2;
218 }
219
220 int
221 md_mbr_use_wholedisk(mbr_info_t *mbri)
222 {
223 return mbr_use_wholedisk(mbri);
224 }
225
226 int
227 md_pre_mount()
228 {
229 return 0;
230 }
231