1 /* $NetBSD: md.c,v 1.13 2026/01/09 12:39:41 skrll 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 -- zaurus machine specific routines */ 36 37 #include <sys/param.h> 38 #include <sys/sysctl.h> 39 40 #include <stdio.h> 41 #include <util.h> 42 43 #include "defs.h" 44 #include "endian.h" 45 #include "md.h" 46 #include "msg_defs.h" 47 #include "menu_defs.h" 48 49 void 50 md_init(void) 51 { 52 } 53 54 void 55 md_init_set_status(int flags) 56 { 57 static const int mib[2] = {CTL_KERN, KERN_VERSION}; 58 size_t len; 59 char *version; 60 61 /* check INSTALL kernel name to select an appropriate kernel set */ 62 /* XXX: hw.cpu_model has a processor name on arm ports */ 63 sysctl(mib, 2, NULL, &len, NULL, 0); 64 version = malloc(len); 65 if (version == NULL) 66 return; 67 sysctl(mib, 2, version, &len, NULL, 0); 68 if (strstr(version, "C700") != NULL) 69 set_kernel_set(SET_KERNEL_C700); 70 free(version); 71 } 72 73 bool 74 md_get_info(struct install_partition_desc *install) 75 { 76 int res; 77 78 if (pm->no_mbr || pm->no_part) 79 return true; 80 81 again: 82 if (pm->parts == NULL) { 83 84 const struct disk_partitioning_scheme *ps = 85 select_part_scheme(pm, NULL, true, NULL); 86 87 if (!ps) 88 return false; 89 90 struct disk_partitions *parts = 91 (*ps->create_new_for_disk)(pm->diskdev, 92 0, pm->dlsize, true, NULL); 93 if (!parts) 94 return false; 95 96 pm->parts = parts; 97 if (ps->size_limit > 0 && pm->dlsize > ps->size_limit) 98 pm->dlsize = ps->size_limit; 99 } 100 101 res = set_bios_geom_with_mbr_guess(pm->parts); 102 if (res == 0) 103 return false; 104 else if (res == 1) 105 return true; 106 107 pm->parts->pscheme->destroy_part_scheme(pm->parts); 108 pm->parts = NULL; 109 goto again; 110 } 111 112 int 113 md_make_bsd_partitions(struct install_partition_desc *install) 114 { 115 return make_bsd_partitions(install); 116 } 117 118 /* 119 * any additional partition validation 120 */ 121 bool 122 md_check_partitions(struct install_partition_desc *install) 123 { 124 return true; 125 } 126 127 /* 128 * hook called before writing new disklabel. 129 */ 130 bool 131 md_pre_disklabel(struct install_partition_desc *install, 132 struct disk_partitions *parts) 133 { 134 135 if (parts->parent == NULL) 136 return true; /* no outer partitions */ 137 138 parts = parts->parent; 139 140 msg_display_subst(MSG_dofdisk, 3, parts->disk, 141 msg_string(parts->pscheme->name), 142 msg_string(parts->pscheme->short_name)); 143 144 /* write edited "MBR" onto disk. */ 145 if (!parts->pscheme->write_to_disk(parts)) { 146 msg_display(MSG_wmbrfail); 147 process_menu(MENU_ok, NULL); 148 return false; 149 } 150 return true; 151 } 152 153 /* 154 * hook called after writing disklabel to new target disk. 155 */ 156 bool 157 md_post_disklabel(struct install_partition_desc *install, 158 struct disk_partitions *parts) 159 { 160 return true; 161 } 162 163 /* 164 * hook called after upgrade() or install() has finished setting 165 * up the target disk but immediately before the user is given the 166 * ``disks are now set up'' message. 167 */ 168 int 169 md_post_newfs(struct install_partition_desc *install) 170 { 171 struct mbr_sector pbr; 172 char adevname[STRSIZE]; 173 ssize_t sz; 174 int fd = -1; 175 176 snprintf(adevname, sizeof(adevname), "/dev/r%sa", pm->diskdev); 177 fd = open(adevname, O_RDWR); 178 if (fd < 0) 179 goto out; 180 181 /* Read partition boot record */ 182 sz = pread(fd, &pbr, sizeof(pbr), 0); 183 if (sz != sizeof(pbr)) 184 goto out; 185 186 /* Check magic number */ 187 if (pbr.mbr_magic != le16toh(MBR_MAGIC)) 188 goto out; 189 190 #ifdef NETBSD_MAJOR 191 #define OSNAME "NetBSD" NETBSD_MAJOR 192 #else 193 #define OSNAME "NetBSD60" 194 #endif 195 /* Update oemname */ 196 memcpy(&pbr.mbr_oemname, OSNAME, sizeof(OSNAME) - 1); 197 198 /* Clear BPB */ 199 memset(&pbr.mbr_bpb, 0, sizeof(pbr.mbr_bpb)); 200 201 /* write-backed new partition boot record */ 202 (void)pwrite(fd, &pbr, sizeof(pbr), 0); 203 204 out: 205 if (fd >= 0) 206 close(fd); 207 return 0; 208 } 209 210 int 211 md_post_extract(struct install_partition_desc *install, bool upgrade) 212 { 213 return 0; 214 } 215 216 void 217 md_cleanup_install(struct install_partition_desc *install) 218 { 219 #ifndef DEBUG 220 enable_rc_conf(); 221 #endif 222 } 223 224 int 225 md_pre_update(struct install_partition_desc *install) 226 { 227 return 1; 228 } 229 230 /* Upgrade support */ 231 int 232 md_update(struct install_partition_desc *install) 233 { 234 md_post_newfs(install); 235 return 1; 236 } 237 238 int 239 md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet) 240 { 241 return 2; 242 } 243 244 bool 245 md_parts_use_wholedisk(struct disk_partitions *parts) 246 { 247 248 return parts_use_wholedisk(parts, 0, NULL); 249 } 250 251 252 int 253 md_pre_mount(struct install_partition_desc *install, size_t ndx) 254 { 255 return 0; 256 } 257 258 bool 259 md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri) 260 { 261 return false; /* no change, no need to write back */ 262 } 263 264 #ifdef HAVE_GPT 265 bool 266 md_gpt_post_write(struct disk_partitions *parts, part_id root_id, 267 bool root_is_new, part_id efi_id, bool efi_is_new) 268 { 269 /* no GPT boot support, nothing needs to be done here */ 270 return true; 271 } 272 #endif 273 274